Chapter 6: Data Structures / Lesson 31

Lists

Introduction to Lists

Lists are one of Python's most versatile data structures. They allow you to store multiple items in a single variable, making it easy to work with collections of data. Lists are ordered, changeable (mutable), and can contain duplicate values.

Lists are perfect for storing sequences of items like shopping lists, scores, names, or any collection of related data that you need to access, modify, or iterate over.

Creating Lists

Lists are created using square brackets [] with items separated by commas. Lists can contain any data type, including mixed types:

creating_lists.py
# Empty list empty_list = [] # List of strings fruits = ["apple", "banana", "cherry"] # List of numbers numbers = [1, 2, 3, 4, 5] # Mixed data types mixed = ["Alice", 25, 5.6, True] # List with duplicate values scores = [85, 90, 85, 95] print(fruits) # ['apple', 'banana', 'cherry'] print(numbers) # [1, 2, 3, 4, 5] print(mixed) # ['Alice', 25, 5.6, True]

Accessing List Items

You can access list items by their index (position). Python uses zero-based indexing, meaning the first item is at index 0:

accessing_items.py
# Access items by index fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Output: apple (first item) print(fruits[1]) # Output: banana (second item) print(fruits[2]) # Output: cherry (third item) # Negative indexing (from the end) print(fruits[-1]) # Output: cherry (last item) print(fruits[-2]) # Output: banana (second to last) # List length print(len(fruits)) # Output: 3

Modifying Lists

Lists are mutable, meaning you can change their contents after creation:

modifying_lists.py
# Change an item fruits = ["apple", "banana", "cherry"] fruits[1] = "orange" print(fruits) # ['apple', 'orange', 'cherry'] # Add items (we'll learn methods in next lesson) fruits.append("grape") print(fruits) # ['apple', 'orange', 'cherry', 'grape'] # Lists can grow and shrink numbers = [1, 2, 3] numbers.append(4) print(numbers) # [1, 2, 3, 4]

List Slicing

Slicing allows you to get a subset of a list by specifying a range of indices:

slicing.py
# List slicing numbers = [0, 1, 2, 3, 4, 5] print(numbers[1:4]) # [1, 2, 3] (indices 1 to 3) print(numbers[:3]) # [0, 1, 2] (from start to index 2) print(numbers[3:]) # [3, 4, 5] (from index 3 to end) print(numbers[::2]) # [0, 2, 4] (every 2nd item) print(numbers[-3:]) # [3, 4, 5] (last 3 items) # Slicing creates a new list subset = numbers[1:4] print(subset) # [1, 2, 3]

Iterating Over Lists

You can loop through lists using for loops:

iterating.py
# Loop through list items fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Loop with index using enumerate for index, fruit in enumerate(fruits): print(f"{index}: {fruit}") # Loop through numbers numbers = [10, 20, 30] for num in numbers: print(num * 2) # 20, 40, 60

Checking if Item Exists

Use the in keyword to check if an item exists in a list:

checking_items.py
# Check if item exists fruits = ["apple", "banana", "cherry"] if "apple" in fruits: print("Apple is in the list") if "orange" not in fruits: print("Orange is not in the list") # Use in conditional statements item = "banana" if item in fruits: print(f"{item} found!")

Best Practices

✅ List Usage Tips

• Use lists for ordered collections of items

• Lists can contain any data type, including other lists

• Remember: indices start at 0, not 1

• Use negative indices to access from the end

• Lists are mutable - you can modify them

💡 Important Notes

• Lists use square brackets []

• Items are separated by commas

• Lists preserve order (items stay in the order you add them)

• Lists can contain duplicate values

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...