Python List Type

Definition
A list is a collection data type in Python that is ordered and mutable, allowing duplicate elements. Lists can contain elements of different types, but they are typically used to store collections of similar items.
How to declare a list?
To declare an empty list, you can use square brackets []
or the list()
function:
empty_list = []
empty_list_alternative = list()
To declare a list with initial values, you can specify the values inside the square brackets, separated by commas:
numbers = [1, 2, 3]
fruits = ["apple", "banana", "cherry"]
mixed_list = [1, "apple", 3.14]
How to access elements in a list?
You can access elements in a list using indexing. The index is zero-based, so the first element has an index of 0:
numbers = [1, 2, 3]
first_number = numbers[0] # 1
second_number = numbers[1] # 2
last_number = numbers[-1] # 3 (negative index accesses from the end)
How to add elements to an existing list?
You can add elements to a list using methods like append()
, extend()
, or insert()
:
insert()
adds an element at a specific position:
numbers = [1, 2, 3]
numbers.insert(1, 1.5)
# numbers is now [1, 1.5, 2, 3]
extend()
adds multiple elements to the end of the list:
numbers = [1, 2, 3]
numbers.extend([4, 5])
# numbers is now [1, 2, 3, 4, 5]
append()
adds a single element to the end of the list:
numbers = [1, 2, 3]
numbers.append(4)
# numbers is now [1, 2, 3, 4]
How to iterate over elements in a list?
You can iterate over the elements in a list using a for
loop:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
How to remove an element from a list?
You can remove elements from a list using methods like remove()
, pop()
, or del
:
del
removes an element at a specific index:
fruits = ["apple", "banana", "cherry"]
del fruits[1]
# fruits is now ["apple", "cherry"]
pop()
removes and returns an element at a specific position (default is the last element):
fruits = ["apple", "banana", "cherry"]
fruit = fruits.pop() # 'cherry'
# fruits is now ["apple", "banana"]
remove()
removes the first occurrence of a specified value:
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
# fruits is now ["apple", "cherry"]
List operations (concatenation, etc.)
- Slicing: You can extract a sublist using slice notation:
numbers = [1, 2, 3, 4, 5]
sublist = numbers[1:4]
# sublist is [2, 3, 4]
- Repetition: You can repeat lists using the
*
operator:
list1 = [1, 2]
repeated_list = list1 * 2
# repeated_list is [1, 2, 1, 2]
- Concatenation: You can concatenate lists using the
+
operator:
list1 = [1, 2]
list2 = [3, 4]
combined_list = list1 + list2
# combined_list is [1, 2, 3, 4]
List Comprehensions
List comprehensions provide a concise way to create lists. The syntax is [expression for item in iterable if condition]
:
numbers = [1, 2, 3, 4, 5]
squares = [n**2 for n in numbers]
# squares is [1, 4, 9, 16, 25]
even_squares = [n**2 for n in numbers if n % 2 == 0]
# even_squares is [4, 16]
Best Practices with Lists
- Use list comprehensions for concise and readable code.
- Avoid using lists for large data sets if performance is a concern; consider using more efficient data structures like
deque
from thecollections
module. - When accessing elements, handle possible
IndexError
exceptions with proper error checking. - Be cautious with mutable default arguments (e.g., default list arguments in functions).
Practice Problem
Write a Python function that takes a list of integers and returns a new list containing the squares of the even numbers from the original list.
def square_even_numbers(numbers):
return [n**2 for n in numbers if n % 2 == 0]
# Example usage
numbers = [1, 2, 3, 4, 5]
result = square_even_numbers(numbers)
print(result) # Output: [4, 16]