Understanding List Indexing in Python
In this section, we will delve into the world of list indexing, exploring its various aspects step-by-step using clear and relatable examples. Here is the list that we will cover in this section:
- Changing the Value of an Element
- Copying Values Between Elements
- Accessing Individual Elements
- Printing the Entire List
- Determining the Length of a List
- Removing Element from a List
- Negative Indices
- Checking Element Presence
Changing the Value of an Element:
Let's start by understanding how to change the value of a chosen element in a list. Consider the following example:
numbers = [10, 5, 7, 2, 1]
print("Original list contents:", numbers)
numbers[0] = 111
print("New list contents:", numbers)
In this example, we have a list called numbers
with five elements. To change the value of the first element, we use indexing: numbers[0] = 111
assigns the new value to the first element of the list.
As you can see, the value of the first element has been updated to 111.
Copying Values Between Elements:
Now, let's explore how to copy the value of the fifth element to the second element:
numbers = [10, 5, 7, 2, 1]
print("Original list contents:", numbers)
numbers[0] = 111
print("\nPrevious list contents:", numbers)
numbers[1] = numbers[4] # Copying the value of the fifth element to the second.
print("New list contents:", numbers)
Here, we first update the first element to 111
and then assign the value of the fifth element (1
) to the second element using indexing (numbers[1] = numbers[4]
).
Note that, the value inside the brackets which select one element of the list is called an index, while the operation of selecting an element from the list is known as indexing. Also, all the indices used so far are literals. Their values are fixed at runtime, but any expression can be the index, too. For instance:
numbers = [10, 5, 7, 2, 1]
i = 1
j = 2
print(numbers[i+j]) #The output will be 2
Accessing Individual Elements:
In addition to modifying elements, we can access individual elements in a list using indexing. For example, to access the first element:
numbers = [10, 5, 7, 2, 1]
print(numbers[0]) # Accessing the 1st element of the list which is 10
Assuming the previous operations have been completed successfully, running the above code will output 111
to the console.
Printing the Entire List:
To print the entire list, we can simply use:
numbers = [10, 5, 7, 2, 1
print(numbers) # Printing the whole list.
The output will be displayed as a list, such as [111, 1, 7, 2, 1]
.
Determining the Length of a List:
The length of a list can vary during execution, with elements being added or removed dynamically. To check the current length of a list, we use the len()
function:
numbers = [10, 5, 7, 2, 1]
print(len(numbers)) # Printing the length of the list which is 5
The len()
function returns the number of elements currently stored in the list.
Removing Elements from a List:
To remove an element from a list, we use the del statement:
numbers = [10, 5, 7, 2, 1]
del numbers[1] # Removing the second element (5) from the list.
print(len(numbers)) # Printing the new length of the list which is 4
print(numbers) # Printing the updated list: [10, 7, 2, 1]
After removing the second element, the length of the list decreases, and the list itself is modified accordingly.
Negative Indices:
Surprisingly, negative indices are legal in Python and can be useful. The last element of a list has an index of -1
, the second-to-last element has an index of -2
, and so on.
numbers = [10, 5, 7, 2, 1]
print(numbers[-1]) # Accessing the last element of the list.
print(numbers[-2]) # Accessing the second-to-last element of the list.
However, it's important to note that accessing elements beyond the range of the list will result in an error.
Checking Element Presence
In Python, you can check if an element is present in a list using the in
keyword or the not in
keyword. Here's how you can use them:
Using the in
keyword:
my_list = [1, 2, 3, 4, 5]
element = 3
if element in my_list:
print("Element is present in the list")
In this example, we have a list called my_list
and we want to check if the element 3
is present in the list. The in
keyword is used to perform the check. If the element is found in the list, the condition will evaluate to True
, and the corresponding code block will be executed.
Using the not in
keyword:
my_list = [1, 2, 3, 4, 5]
element = 6
if element not in my_list:
print("Element is not present in the list")
In this example, we have a list called my_list
and we want to check if the element 6
is not present in the list. The not in
keyword is used to perform the check. If the element is not found in the list, the condition will evaluate to True
, and the corresponding code block will be executed.
Both in
and not in
are membership operators in Python that can be used to check for the presence or absence of an element in a list (or other iterable objects like strings, tuples, sets, etc.).