Skip to main content

Command Palette

Search for a command to run...

Chapter 2: Introduction to Python Lists

Published
โ€ข3 min read
T

Data & AI Transformation Lead โ€ข 15+ years of experience in AI-driven Quality Engineering, Data Analytics, and Test Automation. โ€ข Expertise in AI/ML, Data Testing, Cloud Automation, ETL Pipelines, and Observability tools. โ€ข Hands-on leader in AI-driven automation, predictive analytics, and cloud data validation. โ€ข Strong technical proficiency in Snowflake, DBT, Power BI, Python, AWS, SQL, and MLOps pipelines. โ€ข Proven success in reducing defects, optimizing performance, and streamlining data workflows. โ€ข Expertise in integrating security into CI/CD pipelines, automated security scanning, vulnerability assessment, and compliance-driven quality engineering. โ€ข Expertise in JMeter, BlazeMeter, Snowflake SQL Profiler, optimizing application & data performance testing for high-scale environments. โ€ข Strong expertise in SAFe, Scrum, and Kanban methodologies, ensuring seamless collaboration between development, testing, and operations teams.

๐Ÿ”น 1. List

  • A collection of elements (numbers, strings, etc.)
fruits = ["apple", "banana", "cherry"]

๐Ÿ”น 2. Subset of List (Indexing)

  • Accessing a single element using its index
print(fruits[1])    # Output: "banana"
print(fruits[-1])   # Output: "cherry"

๐Ÿ”น 3. Slicing

  • Extracting a portion (sublist) using a range of indices
print(fruits[0:2])  # Output: ["apple", "banana"]
print(fruits[1:])   # Output: ["banana", "cherry"]
print(fruits[:2])   # Output: ["apple", "banana"]

๐Ÿ”น 4. Subset Slicing (Nested List)

  • Access part of an inner list or string inside a list
nested = [["room", 10], ["hall", 12]]
print(nested[0][0])      # Output: "room"
print(nested[0][0][1:])  # Output: "oom" โ†’ slicing string inside list

๐Ÿ”น 5. Referencing vs Copying

  • Referencing: changing one affects the other
a = [1, 2, 3]
b = a
b[0] = 100
print(a)  # Output: [100, 2, 3] โ€“ both point to same list
  • Copying: use slicing or copy()
c = a[:]     # or c = a.copy()
c[0] = 50
print(a)     # Output: [100, 2, 3]
print(c)     # Output: [50, 2, 3]

๐Ÿ”น 6. List Concatenation

  • Adding elements to a list using the + operator creates a new list.

โœ… Step-by-step Explanation:

areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
         "bedroom", 10.75, "bathroom", 10.50]
  • This is your original list with alternating room names and their sizes.

areas_1 = areas + ["poolhouse", 24.5]
  • A new list areas_1 is created by adding "poolhouse" and its area 24.5 to the end of areas.

  • areas remains unchanged (since + creates a copy).


areas_2 = areas_1 + ["garage", 15.45]
  • Another new list areas_2 is created by adding "garage" and its area 15.45 to areas_1.

  • Again, this doesn't modify areas_1.


๐Ÿ”น 7. List Growth Through Concatenation

  • Each time, a new list is created, so:

    • areas: original list

    • areas_1: original + poolhouse

    • areas_2: areas_1 + garage


โœ… Summary Table:

ListContents AddedTotal Length
areasOriginal 10 elements10
areas_1+ "poolhouse", 24.512
areas_2+ "garage", 15.4514

๐Ÿ”น 8. BUILT IN PYTHON FUNCTIONS

CategoryFunctionsPurpose
Type Conversionint(), float(), str(), bool()Convert data types
Type Checkingtype(), isinstance()Check the type of a variable
Sequence Operationslen(), max(), min(), sum()Work on lists, tuples, etc.
Mathematicalabs(), round(), pow()Basic math operations
I/O Operationsprint(), input()Display or receive user input
Functional Programmingmap(), filter(), zip(), sorted()Apply functions to iterables
Object Introspectiondir(), help(), id()Examine objects and their properties
Memory & IDid()Get unique ID of an object
Evaluationeval(), exec()Execute strings as Python code
Miscellaneousany(), all(), enumerate(), range()Utility functions for logic/iteration

LearnPython

Part 1 of 1

LearnPython

More from this blog

M

Mastering AI/ML: Essential Tips and Resources for Aspiring Data Scientists

17 posts

Unlock the power of AI, Machine Learning, Data Science, and Data Engineering.

#AI #MachineLearning #DataScience #DataEngineering #DataAnalytics