Python List - Access Items - list[index], list[range]
Python List - Access Items
To access list items individually, you can use an index just like an array. You can also access a range of items in the list by specifying a range as an index.
In this tutorial, we will go through examples to understand how to access items in a Python list step-by-step.
Examples
1: Access a single item in a given list using index
In this example, we access a single item from the list using an index.
Python Program
my_list = [52, 85, 'apple', 'banana']
# Access 3rd item whose index=2
item = my_list[2]
print(item)Output
appleExplanation (Step-by-Step):
- Define a list
my_listwith four elements:[52, 85, 'apple', 'banana']. - Use
my_list[2]to access the third element of the list. The index starts from 0, so index 2 corresponds to the third item. - Assign the accessed element
'apple'to the variableitem. - Print the value of
item, which is'apple'.
2: Access a range of items in a list using slicing
Here, we use slicing to retrieve a subset of the list.
Python Program
my_list = [52, 85, 41, 'apple', 'banana']
# Access a range of items
sub_list = my_list[1:4]
print(sub_list)Output
[85, 41, 'apple']Explanation (Step-by-Step):
- Define a list
my_listwith five elements:[52, 85, 41, 'apple', 'banana']. - Use slicing
my_list[1:4]to retrieve a sublist. The start index is1, and the stop index is4(exclusive). - The slice includes the items at indices
1,2, and3, which are85,41, and'apple', respectively. - Assign the sublist
[85, 41, 'apple']to the variablesub_list. - Print the value of
sub_list.
3: Access items with negative indexing
Negative indexing allows you to access elements from the end of the list.
Python Program
my_list = [52, 85, 41, 'apple', 'banana']
# Access the last item
last_item = my_list[-1]
# Access the second-to-last item
second_last_item = my_list[-2]
print(last_item)
print(second_last_item)Output
banana
appleExplanation (Step-by-Step):
- Define a list
my_listwith five elements:[52, 85, 41, 'apple', 'banana']. - Use
my_list[-1]to access the last element,'banana', and assign it tolast_item. - Use
my_list[-2]to access the second-to-last element,'apple', and assign it tosecond_last_item. - Print the values of
last_itemandsecond_last_item.
4: Access every nth item using slicing
Slicing with a step value allows you to retrieve every nth element.
Python Program
my_list = [10, 20, 30, 40, 50, 60]
# Access every 2nd item
every_second_item = my_list[::2]
print(every_second_item)Output
[10, 30, 50]Explanation (Step-by-Step):
- Define a list
my_listwith six elements:[10, 20, 30, 40, 50, 60]. - Use slicing
my_list[::2]. Here, the step value2means select every second element starting from the beginning. - The slice includes
10,30, and50. - Assign the resulting sublist
[10, 30, 50]to the variableevery_second_item. - Print the value of
every_second_item.
5: Access items using a loop and condition
Here, we use a loop and condition to filter items from the list.
Python Program
my_list = [1, 2, 3, 4, 5, 6]
# Access even numbers
even_numbers = [item for item in my_list if item % 2 == 0]
print(even_numbers)Output
[2, 4, 6]Explanation (Step-by-Step):
- Define a list
my_listwith six elements:[1, 2, 3, 4, 5, 6]. - Use a list comprehension to iterate through each element in
my_list. - For each item, check if it is divisible by
2(even number). - Include the items
2,4, and6in the new listeven_numbers. - Print the value of
even_numbers.
Summary
In this tutorial, we learned how to access individual elements, slices, and filtered elements from a Python list using indexes, slicing, and conditions with step-by-step explanations.