Python Lists
A list in Python is an ordered collection of items that can store multiple elements. Lists are mutable, meaning their elements can be changed after the list is created. Lists can store elements of any data type, including numbers, strings, and even other lists.
What is a List?
A list in Python is defined by enclosing the elements in square brackets []
, separated by commas. Lists are ordered, changeable, and allow duplicate values.
Syntax
# Creating a list in Python
my_list = [1, 2, 3, 4, 5] # A list of integers
string_list = ["apple", "banana", "cherry"] # A list of strings
mixed_list = [1, "apple", 3.14, True] # A list with mixed data types
Explanation:
In the above example:my_list
contains integers.string_list
contains strings.mixed_list
contains different data types: an integer, a string, a float, and a boolean.
Accessing Elements in a List
Elements in a list can be accessed using indexing. The index starts at 0 for the first element and goes up to n-1
for the n
th element. Negative indices can be used to access elements from the end of the list.
Accessing elements in a list
my_list = [1, 2, 3, 4, 5]
# Access the first element
first_element = my_list[0] # Index 0 corresponds to the first element
print("First element:", first_element)
# Access the last element
last_element = my_list[-1] # Index -1 corresponds to the last element
print("Last element:", last_element)
# Access elements from index 1 to 2 (slice from index 1 to 3, but excluding 3)
substring = my_list[1:3]
print("Substring (elements from index 1 to 2):", substring)
Output
Explanation:
In this example:my_list[0]
accesses the first element (1).my_list[-1]
accesses the last element (5).my_list[1:3]
slices the list from index 1 to 2, returning a sublist [2, 3].
Modifying Lists
Since lists are mutable, you can modify their elements by directly assigning new values to specific indices. You can also add or remove elements from a list.
Modifying an Element
# Modifying elements in a list
my_list = [1, 2, 3, 4, 5]
# Modify the third element (index 2) to 10
my_list[2] = 10
# Print the updated list
print("Updated list:", my_list)
Output
Explanation:
Here,my_list[2] = 10
changes the third element from 3 to 10. The updated list will be [1, 2, 10, 4, 5].
Adding Elements to a List
my_list = [1, 2, 3, 4]
# Add 5 to the end of the list
my_list.append(5)
print("After append:", my_list)
# Insert 10 at index 2
my_list.insert(2, 10)
print("After insert:", my_list)
Output
Explanation:
In the above example:append(5)
adds 5 to the end of the list.insert(2, 10)
inserts 10 at index 2, shifting the rest of the elements to the right.
Removing Elements from a List
my_list = [1, 2, 3, 4, 5]
# Remove the first occurrence of 3
my_list.remove(3)
print("After remove(3):", my_list)
# Remove the element at index 1 (which is 2)
my_list.pop(1)
print("After pop(1):", my_list)
Output
Explanation:
Here:remove(3)
removes the first occurrence of the value 3 from the list.pop(1)
removes the element at index 1, which is 2.
List Methods
Python provides several built-in methods to perform common operations on lists. These methods allow us to manipulate and interact with lists more efficiently.
Examples:
List Methods
my_list = [1, 2, 3, 4, 5]
# Get the length of the list
length = len(my_list)
print("Length of the list:", length)
# Sort the list in ascending order
my_list.sort() # Modifies the list in place
print("After sort:", my_list)
# Reverse the order of elements
my_list.reverse() # Modifies the list in place
print("After reverse:", my_list)
# Count how many times 1 appears in the list
count_ones = my_list.count(1)
print("Count of 1 in the list:", count_ones)
Output
Explanation:
In the example:len(my_list)
returns the number of elements in the list.sort()
sorts the list in ascending order.reverse()
reverses the order of the list.count(1)
returns how many times 1 appears in the list.
Nested Lists
Python lists can contain other lists as elements. These are known as nested lists. You can access elements in nested lists using multiple indices.
Example
# Nested lists
nested_list = [[1, 2], [3, 4], [5, 6]]
# Access the first sublist
first_sublist = nested_list[0]
print("First sublist:", first_sublist)
# Access the second element of the first sublist
first_element_of_sublist = nested_list[0][1]
print("Second element of the first sublist:", first_element_of_sublist)
Output
Explanation:
In the example:nested_list[0]
accesses the first sublist [1, 2].nested_list[0][1]
accesses the second element of the first sublist, which is 2.