Python Lesson 10 – Lists | Dataplexa

Lists in Python

In real programming, applications rarely work with a single value. Systems manage collections such as user names, product prices, course modules, or transaction records. Python solves this problem using lists, which allow storing multiple related values inside one variable.

Lists are one of the most powerful and frequently used data structures in Python. Almost every professional Python system — automation tools, machine learning pipelines, APIs, dashboards, and backend services — depends on lists for managing grouped data efficiently.

Understanding Lists

A list is an ordered and mutable collection of values. Ordered means items keep their position, and mutable means values can be modified after creation. Lists allow mixing different data types together when required.


# Creating lists with different data types

numbers = [10, 20, 30]
courses = ["Python", "SQL", "AI"]
flags = [True, False, True]

# Printing lists
print(numbers)
print(courses)
print(flags)
[10, 20, 30] ['Python', 'SQL', 'AI'] [True, False, True]
  • Each print() statement executes separately.
  • Output appears vertically exactly like terminal execution.
  • Lists preserve insertion order.
  • Square brackets define a list.

Why Lists Are Needed

Without lists, programmers would need hundreds of variables to manage repeated information. Lists enable looping, searching, filtering, and updating data automatically.

For example, an e-commerce system stores thousands of product prices inside lists instead of creating separate variables.

Accessing Elements Using Index

Each list item has a position called an index. Python indexing begins from zero.


# Accessing list items

modules = ["Python", "SQL", "AI", "Cloud"]

print(modules[0])
print(modules[2])
Python AI
  • Index 0 accesses the first element.
  • Each print produces output on a new line.
  • Indexing allows precise data retrieval.

Negative Indexing

Python allows accessing elements from the end using negative indexes. This helps when retrieving latest records.


# Accessing elements from end

modules = ["Python", "SQL", "AI", "Cloud"]

print(modules[-1])
print(modules[-2])
Cloud AI
  • -1 represents last element.
  • Useful in logs, queues, and recent activity tracking.

Slicing Lists

Slicing extracts a portion of data. Data analysts and ML engineers frequently use slicing while processing datasets.


# Extracting subset of list

values = [10, 20, 30, 40, 50]

print(values[0:3])
print(values[2:])
[10, 20, 30] [30, 40, 50]
  • Start index included.
  • End index excluded.
  • Slicing creates a new list.

Updating List Values

Because lists are mutable, values can be modified without recreating the structure.


# Updating element

tools = ["Python", "SQL", "AI"]

tools[1] = "Data Analytics"

print(tools)
['Python', 'Data Analytics', 'AI']
  • Existing value replaced using index.
  • Remaining items stay unchanged.

Adding Elements

append()


# Adding element at end

tasks = ["Login", "Study"]

tasks.append("Practice")

print(tasks)
['Login', 'Study', 'Practice']

insert()


# Insert element at position

steps = ["Start", "Finish"]

steps.insert(1, "Process")

print(steps)
['Start', 'Process', 'Finish']

Removing Elements


# Removing element using pop()

items = ["Pen", "Book", "Bottle"]

removed = items.pop(1)

print(removed)
print(items)
Book ['Pen', 'Bottle']

Looping Through Lists

Loops allow automatic processing of list data instead of manual repetition.


# Iterating list values

modules = ["Python", "SQL", "AI"]

for module in modules:
    print(module)
Python SQL AI

Real-World Example: Cart Total


# Calculating cart total

prices = [299, 499, 199]

total = 0

for price in prices:
    total += price

print(total)
997

Practice

Which method adds an element to the end of a list?



What index represents the first list element?



Quick Quiz

Lists in Python are:





Which brackets define a list?





Lists Summary

ConceptDescription
ListOrdered mutable collection
IndexingAccess using position
SlicingExtract subset
append()Add element
insert()Add at position
pop()Remove by index
LoopingProcess items automatically

Next Up: Tuples in Python.