To fill a list in Python, you can start with an empty list and use methods like append() or extend(), use a for loop, or create it concisely with a list comprehension. The best method depends on whether you are adding a single item, multiple items, or generating a list programmatically.
1. The append() method
The append() method adds a single element to the end of a list. It's an "in-place" operation, meaning it modifies the existing list and does not return a new one. This is a common method for building a list iteratively inside a loop.
Basic usage
You start with an empty list and add items one by one.
my_list = []
my_list.append('apple')
my_list.append('banana')
my_list.append('cherry')
print(my_list)
# Output: ['apple', 'banana', 'cherry']
Use code with caution.
Using append() with a for loop
This approach is used to build a list where the contents are generated within a loop.
squares = []
for i in range(5):
squares.append(i * i)
print(squares)
# Output: [0, 1, 4, 9, 16]
Use code with caution.
2. The extend() method
The extend() method adds all items from an iterable (like another list, tuple, or set) to the end of the current list. This is useful for combining lists or adding multiple items at once.
Basic usage
my_list = ['apple', 'banana']
other_fruits = ['orange', 'grape']
my_list.extend(other_fruits)
print(my_list)
# Output: ['apple', 'banana', 'orange', 'grape']
Use code with caution.
append() vs. extend()
append() adds its argument as a single element, while extend() unpacks the argument and adds each element individually.
# Using append()
list_a = [1, 2, 3]
list_a.append([4, 5])
print(list_a)
# Output: [1, 2, 3, [4, 5]]
# Using extend()
list_b = [1, 2, 3]
list_b.extend([4, 5])
print(list_b)
# Output: [1, 2, 3, 4, 5]
Use code with caution.
3. List comprehension
List comprehensions offer a concise and often more efficient way to create lists from existing iterables.
Basic list comprehension
This is a more compact alternative to the for loop with append().
# Create a list of squared numbers
squares = [i * i for i in range(5)]
print(squares)
# Output: [0, 1, 4, 9, 16]
Use code with caution.
List comprehension with a conditional filter
You can add an if clause to filter elements.
# Get a list of even numbers
even_numbers = [i for i in range(10) if i % 2 == 0]
print(even_numbers)
# Output: [0, 2, 4, 6, 8]
Use code with caution.
4. The insert() method
The insert() method adds an element at a specific index, shifting subsequent elements. It's less efficient than append() for large lists.
Basic usage
my_list = ['apple', 'cherry']
my_list.insert(1, 'banana')
print(my_list)
# Output: ['apple', 'banana', 'cherry']
Use code with caution.
5. Concatenation with the + operator
Combining two or more lists with + creates a new list without modifying the originals.
Basic usage
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list)
# Output: [1, 2, 3, 4, 5, 6]
print(list1)
# Output: [1, 2, 3] (original list is unchanged)
Use code with caution.
6. Duplication with the * operator
The * operator repeats a value in a list.
Basic usage
# Create a list of 5 zeros
zeros = [0] * 5
print(zeros)
# Output: [0, 0, 0, 0, 0]
# Be cautious with mutable objects!
# All elements will reference the same object.
nested_list = [[]] * 3
nested_list[0].append(1)
print(nested_list)
# Output: [[1], [1], [1]] (appending to one list affects all)
Use code with caution.
7. The list() constructor
The list() constructor creates a new list from any iterable.
Creating a list from a string
my_string = "hello"
my_list = list(my_string)
print(my_list)
# Output: ['h', 'e', 'l', 'l', 'o']
Use code with caution.
Creating a list from a tuple
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list)
# Output: [1, 2, 3]
Use code with caution.
When to use each method
| Method | Best for | Considerations |
|---|---|---|
append() |
Building a list iteratively. | Adds a single element; modifies the list in place. |
extend() |
Adding all elements from an existing iterable. | Modifies the list in place; more efficient than repeated append(). |
| List Comprehension | Creating a new list from an iterable concisely. | Highly efficient and "Pythonic". |
insert() |
Adding an element at a specific position. | Less efficient due to element shifting. |
+ operator |
Concatenating two lists to produce a new one. | Creates a new list each time, less memory-efficient for iterative building. |
* operator |
Initializing a list with repeated values. | Be careful with mutable objects. |
list() constructor |
Converting other iterables into a list. | Straightforward for type conversion. |