Lists in Python | Python Course | Dataplexa

Lists in Python

In real programs, you almost never work with just one value at a time. A social media app stores thousands of posts. A shopping website manages hundreds of products. A data analysis tool processes millions of records. Python handles all of this using lists — one of the most important and widely used data structures in the language.

A list is an ordered collection of values stored inside a single variable. You can put as many items as you need into a list, change them at any time, add new ones, remove old ones, and loop through all of them automatically. Understanding lists well is one of the most valuable skills you can build as a Python programmer.

Creating a List

You create a list by putting values inside square brackets [], separated by commas. A list can hold numbers, text, booleans, or a mix of different types.

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

print(numbers)
print(courses)
print(flags)
[10, 20, 30] ['Python', 'SQL', 'AI'] [True, False, True]
  • Square brackets [] define a list.
  • Items inside are separated by commas.
  • Lists keep their order — items always appear in the order you put them in.
  • A list can be empty: my_list = []

Accessing Items by Index

Every item in a list has a position number called an index. Counting starts at 0, so the first item is at index 0, the second at index 1, and so on. You access an item by putting its index inside square brackets.

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

print(modules[0])   # first item
print(modules[2])   # third item
print(modules[-1])  # last item (negative index)
Python AI Cloud
  • Index 0 is always the first item.
  • Index -1 is always the last item — useful when you do not know how many items the list has.
  • Accessing an index that does not exist causes an IndexError.

Slicing a List

Slicing lets you extract a portion of a list — a range of items — without changing the original list. The format is list[start:end] where the start is included and the end is excluded.

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

print(values[0:3])   # first three items
print(values[2:])    # from index 2 to the end
print(values[:3])    # from the start to index 2
[10, 20, 30] [30, 40, 50] [10, 20, 30]
  • The end index is never included in the result.
  • Leaving the start empty means start from the beginning.
  • Leaving the end empty means go to the last item.
  • Slicing creates a new list — the original is unchanged.

Updating a List Item

Because lists are mutable — meaning they can be changed — you can update any item just by assigning a new value to its index position.

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

tools[1] = "Data Analytics"   # replace SQL with Data Analytics

print(tools)
['Python', 'Data Analytics', 'AI']
  • Only the item at index 1 changed. Everything else stays the same.
  • This is different from strings, which are immutable and cannot be changed in place.

Adding Items to a List

Python gives you two ways to add items to a list. append() adds to the end. insert() adds at a specific position.

tasks = ["Login", "Study"]

# Add to the end
tasks.append("Practice")
print(tasks)

# Add at a specific position
tasks.insert(1, "Review Notes")
print(tasks)
['Login', 'Study', 'Practice'] ['Login', 'Review Notes', 'Study', 'Practice']
  • append() is faster and simpler when you just need to add to the end.
  • insert(index, value) places the item at the position you specify and shifts everything else to the right.

Removing Items from a List

Python has several ways to remove items. pop() removes by index and returns the removed item. remove() removes by value.

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

# Remove by index — pop() returns the removed item
removed = items.pop(1)
print("Removed:", removed)
print("List after pop:", items)

# Remove by value
items.remove("Pen")
print("List after remove:", items)
Removed: Book List after pop: ['Pen', 'Bottle'] List after remove: ['Bottle']
  • pop(index) removes the item at that index and gives it back to you.
  • pop() with no index removes the last item.
  • remove(value) removes the first item that matches the value.

Useful List Methods

Python lists come with many built-in methods that make common tasks quick and easy.

numbers = [3, 1, 4, 1, 5, 9, 2, 6]

print(len(numbers))         # count of items
print(numbers.count(1))     # how many times 1 appears
print(sorted(numbers))      # sorted copy (original unchanged)
print(min(numbers))         # smallest value
print(max(numbers))         # largest value
print(sum(numbers))         # total of all values
8 2 [1, 1, 2, 3, 4, 5, 6, 9] 1 9 31
  • len() — works on any collection, not just lists.
  • sorted() returns a new sorted list without changing the original. Use list.sort() to sort in place.
  • min(), max(), and sum() work on lists of numbers.

Looping Through a List

The most common thing you will do with a list is go through every item one at a time using a for loop. This lets you process, display, or calculate with each item automatically.

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

for module in modules:
    print("Now studying:", module)
Now studying: Python Now studying: SQL Now studying: AI

Checking if an Item Exists

Use the in keyword to check whether a value exists somewhere in the list. This returns True or False.

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

print("Python" in courses)    # True
print("Java" in courses)      # False

if "Python" in courses:
    print("Python course is available")
True False Python course is available

Real World Example — Shopping Cart Total

This is one of the most practical uses of a list. A shopping cart holds multiple item prices, and a loop adds them all up to produce the total.

prices = [299, 499, 199, 149]

total = 0

for price in prices:
    total += price

print("Cart Total:", total)
print("Number of items:", len(prices))
print("Most expensive item:", max(prices))
Cart Total: 1146 Number of items: 4 Most expensive item: 499

Common List Mistakes

  • Index out of range — accessing list[5] when the list only has 4 items causes an IndexError. Always check the length first if you are unsure.
  • Confusing = with a copy — writing new_list = my_list does NOT create a separate copy. Both variables point to the same list. Use new_list = my_list.copy() to create an independent copy.
  • Modifying a list while looping through it — adding or removing items inside a loop that goes through the same list can cause items to be skipped. Loop through a copy instead.

Practice

Which method adds a new item to the end of a list?



What index number represents the first item in a list?



Which method removes an item from a list by its index and returns it?



Quick Quiz

Lists in Python are described as what?





Which type of brackets are used to define a list?





If courses = ["Python", "SQL"], what does "Python" in courses return?





NEXT UP
Tuples in Python
Learn about tuples — ordered collections that cannot be changed. Understand when to use a tuple instead of a list and how immutability protects your data.