List in Python
Good morning! Today, I’m going to share what I’ve been learning about lists in Python.
I’ll explain what a list is, how to add items to it, how to modify an item, and how to delete an item from a list.
What is a List in Python?
A list in Python is a data type that allows you to store multiple items in a single variable. For example, we can create a variable that holds just one name, but a list lets us store multiple names or numbers together.
Think of a list as a container that can hold multiple items of different types, such as numbers, strings, or Boolean values.
How to Create a List
To create a list in Python, we use square brackets []. Here’s an example:
names = ["Peter", "Luke", "Avery"]Lists with Different Data Types
You can create a list that holds only numbers:
numbers = [1, 2, 5, 10, 29]Or, you can mix different data types (numbers, strings, Booleans) in a single list:
everything_list = [1, "Luke", True]Understanding List Indexing
Lists in Python are zero-indexed, meaning that each item has a position (index), and counting starts at 0 instead of 1.
For example, in this list:
names = ["Peter", "John", "Luke"]"Peter"is at index 0"John"is at index 1"Luke"is at index 2
Accessing an Item in a List
Knowing the index, we can access an item in the list:
print(names[0]) # First item (Peter)
print(names[1]) # Second item (John)Accessing the Last Item in a List
If a list has 6 items, the last index will be 5 (total items - 1):
names[5] # Access the last item if there are 6 itemsPython also provides an easier way to access the last item using negative indexing:
print(names[-1]) # Prints the last itemAdding Items to a List
In Python, we can use the append() function to add an item to the end of a list:
names.append("Mary")
numbers.append(40)Inserting an Item at a Specific Position
Another way to add an item to a list is by using the insert() function.
The insert() function allows us to add an item at a specific position in the list.
For example, if I want to add "Paula" to the second position (index 1), I will do:
names.insert(1, "Paula")This will insert "Paula" at the second position, pushing the existing items to the right.
Updating an Item in a List
To modify an item, we use the list name, specify the index we want to edit, and assign a new value:
names[0] = "Chloe"Now, the first item (index 0) is updated from "Peter" to "Chloe".
Deleting an Item from a List
If you want to delete an item from a list using its index, you can use the del keyword:
del names[1] # Deletes the item at index 1Removing an Item by Value
If you want to remove an item based on its value rather than its index, use the remove() function:
names.remove("Chloe") # Removes "Chloe" from the listSorting a List
If you want to sort the items in a list in ascending order, you can use the sort() function:
names.sort()Important Note:
When you use sort(), the list is modified in place, meaning you won’t have access to the original, unsorted list unless you make a copy beforehand.
If you want to sort a list without modifying the original, use sorted():
sorted_names = sorted(names) # Returns a new sorted listLooping Through a List
You can iterate through a list and print each item using a for loop:
for item in names:
print(item)This will print each item in the list one by one.
Creating a List of Numbers Using range()
One way to create a list of numbers is by using the range() function.
For example:
numbers = list(range(0, 10)) # Creates a list from 0 to 9
print(numbers) # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]You can also iterate through a list of numbers using range():
for num in range(1, 6): # Loops from 1 to 5
print(num)Copying a List
You can copy a list from another list by assigning the old list to a new variable:
new_list = numbers⚠️ Warning:
If you copy a list this way, new_list will still be linked to numbers, meaning that changes in one list will affect the other.
Creating an Independent Copy
To create a separate copy, use slicing ([:]):
new_list = numbers[:] # Creates a new independent copy