Day 5: Lists in Python
Welcome to Day 5 of your 180-day AI and Machine Learning journey! Today, we’ll explore lists, one of the most versatile and commonly used data structures in Python. Lists allow you to store and manipulate collections of data efficiently. By the end of this article, you’ll understand how to create, access, modify, and perform operations on lists.
What Are Lists?
A list is an ordered collection of items that can be of any data type (e.g., integers, strings, or even other lists). Lists are:
Mutable: You can change their content after creation.
Ordered: Items have a defined order, and you can access them using their index.
Creating a List
You can create a list by enclosing items in square brackets [], separated by commas.
Example:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "apple", 3.14, True]Accessing List Items
You can access items in a list using their index. Python uses zero-based indexing, meaning the first item has an index of 0.
Example:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: cherryYou can also use negative indexing to access items from the end of the list:
print(fruits[-1]) # Output: cherry (last item)
print(fruits[-2]) # Output: banana (second last item)Modifying Lists
Lists are mutable, so you can change their content after creation.
Example:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry" # Change the second item
print(fruits) # Output: ['apple', 'blueberry', 'cherry']List Operations
Python provides many built-in methods and operations for working with lists.
1. Adding Items
append(): Adds an item to the end of the list.insert(): Inserts an item at a specific index.extend(): Adds multiple items (from another list) to the end.
Example:
fruits = ["apple", "banana"]
fruits.append("cherry") # ['apple', 'banana', 'cherry']
fruits.insert(1, "blueberry") # ['apple', 'blueberry', 'banana', 'cherry']
fruits.extend(["orange", "mango"]) # ['apple', 'blueberry', 'banana', 'cherry', 'orange', 'mango']2. Removing Items
remove(): Removes the first occurrence of a specific item.pop(): Removes and returns the item at a specific index (or the last item if no index is provided).clear(): Removes all items from the list.
Example:
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana") # ['apple', 'cherry']
fruits.pop(1) # ['apple']
fruits.clear() # []3. Other Useful Methods
len(): Returns the number of items in the list.sort(): Sorts the list in ascending order.reverse(): Reverses the order of the list.index(): Returns the index of the first occurrence of a specific item.count(): Returns the number of times a specific item appears.
Example:
numbers = [3, 1, 4, 1, 5, 9]
print(len(numbers)) # Output: 6
numbers.sort() # [1, 1, 3, 4, 5, 9]
numbers.reverse() # [9, 5, 4, 3, 1, 1]
print(numbers.index(4)) # Output: 2
print(numbers.count(1)) # Output: 2Slicing Lists
You can extract a portion of a list using slicing. The syntax is list[start:stop:step].
Example:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:5]) # Output: [2, 3, 4] (items from index 2 to 4)
print(numbers[:5]) # Output: [0, 1, 2, 3, 4] (items from start to index 4)
print(numbers[5:]) # Output: [5, 6, 7, 8, 9] (items from index 5 to end)
print(numbers[::2]) # Output: [0, 2, 4, 6, 8] (every second item)Practice Exercise
Create a list of your favorite fruits and print the second item.
Add a new fruit to the list and print the updated list.
Remove the last item from the list and print the updated list.
Sort the list in alphabetical order and print it.
Solution:
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Output: banana
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
fruits.pop()
print(fruits) # Output: ['apple', 'banana', 'cherry']
fruits.sort()
print(fruits) # Output: ['apple', 'banana', 'cherry']Key Takeaways
Lists are ordered, mutable collections of items.
Use indexing and slicing to access and modify list items.
Use built-in methods like
append(),remove(),sort(), andreverse()to manipulate lists.
What’s Next?
Tomorrow, we’ll explore tuples, which are similar to lists but immutable. They are useful for storing data that should not change.
Congratulations on completing Day 5! Keep practicing, and see you tomorrow! 🚀


