Python lists are one of the most versatile and widely used data structures. They are mutable, ordered sequences of elements that can contain items of different data types, including other lists.
A list in Python is created by placing all the elements (items) inside square brackets []
, separated by commas.
my_list = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", 3.14, [1, 2, 3]]
Lists can contain elements of different types, including other lists (nested lists).
append(item)
: Add an item to the end of the list.
my_list = [1, 2, 3]
my_list.append(4) # [1, 2, 3, 4]
insert(index, item)
: Insert an item at a specified position.
my_list = [1, 2, 3]
my_list.insert(1, 'a') # [1, 'a', 2, 3]
extend(iterable)
: Extend the list by appending elements from an iterable (e.g., another list).
my_list = [1, 2, 3]
my_list.extend([4, 5]) # [1, 2, 3, 4, 5]
remove(item)
: Remove the first occurrence of an item.
my_list = [1, 2, 3, 2]
my_list.remove(2) # [1, 3, 2]
pop(index=-1)
: Remove and return an item at a specified index (default is the last item).
my_list = [1, 2, 3]
my_list.pop() # Returns 3, my_list becomes [1, 2]
my_list.pop(0) # Returns 1, my_list becomes [2]
clear()
: Remove all items from the list.
my_list = [1, 2, 3]
my_list.clear() # []
Indexing: Access elements by their index. Python uses zero-based indexing.
my_list = [1, 2, 3]
first_item = my_list[0] # 1
last_item = my_list[-1] # 3
Slicing: Access a range of elements using slicing.
my_list = [1, 2, 3, 4, 5]
sub_list = my_list[1:4] # [2, 3, 4]
Index Assignment: Modify an element at a specific index.
my_list = [1, 2, 3]
my_list[1] = 'a' # [1, 'a', 3]
Slicing Assignment: Modify a range of elements.
my_list = [1, 2, 3, 4, 5]
my_list[1:4] = ['a', 'b', 'c'] # [1, 'a', 'b', 'c', 5]
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
count(item)
: Return the number of occurrences of an item in the list.
my_list = [1, 2, 2, 3]
my_list.count(2) # 2
index(item, start=0, end=len(list))
: Return the index of the first occurrence of an item.
my_list = [1, 2, 3]
my_list.index(2) # 1
sort(key=None, reverse=False)
: Sort the list in place.
my_list = [3, 1, 2]
my_list.sort() # [1, 2, 3]
my_list.sort(reverse=True) # [3, 2, 1]
reverse()
: Reverse the elements of the list in place.
my_list = [1, 2, 3]
my_list.reverse() # [3, 2, 1]
copy()
: Return a shallow copy of the list.
my_list = [1, 2, 3]
copy_list = my_list.copy() # [1, 2, 3]
deque
for fast append and pop operations from both ends, or numpy
arrays for numerical data.Modifying Lists While Iterating: Modifying a list while iterating over it can lead to unexpected behavior.
my_list = [1, 2, 3]
for item in my_list:
if item == 2:
my_list.remove(item)
# This might not remove all '2's as expected. Instead, use a list comprehension.
Confusing Copy with Reference: Assigning a list to a new variable doesn't create a copy; it creates a reference.
list1 = [1, 2, 3]
list2 = list1 # list2 is now a reference to list1
list2.append(4)
# Both list1 and list2 will reflect the change [1, 2, 3, 4]
Using Lists for Hashable Collections: Lists are mutable and cannot be used as dictionary keys or elements of a set.
array
module or numpy
arrays.This comprehensive guide should help you understand Python lists, their features, and best practices for using them efficiently in your programs.