Last updated: Apr 9, 2024
Reading timeยท3 min
Use list slicing to print specific items in a list, e.g.
print(my_list[1:3])
.
The print()
function will print the slice of the list starting at the
specified index and going up to, but not including the stop
index.
my_list = ['bobby', 'hadz', '.', 'com'] # โ Print the first item in a list first = my_list[0] print(first) # ๐๏ธ 'bobby' # โ Print the last item in a list last = my_list[-1] print(last) # ๐๏ธ 'com' # โ Print slice containing multiple list items result = my_list[1:3] print(result) # ๐๏ธ ['hadz', '.']
If you need to print a single list item, access the list at the specific index.
my_list = ['bobby', 'hadz', '.', 'com'] first = my_list[0] print(first) # ๐๏ธ 'bobby' last = my_list[-1] print(last) # ๐๏ธ 'com'
You can use the list.index()
method if you need to get the index of a list
item.
my_list = ['bobby', 'hadz', '.', 'com'] variable = 'bobby' index = my_list.index(variable) print(index) # ๐๏ธ 0 print(my_list[index]) # ๐๏ธ 'bobby'
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(my_list) - 1
.
Use list slicing if you need to print multiple items in the list.
my_list = ['bobby', 'hadz', '.', 'com'] # โ print the first 2 items in a list print(my_list[:2]) # ๐๏ธ ['bobby', 'hadz'] # โ print the last 2 items in a list print(my_list[-2:]) # ๐๏ธ ['.', 'com'] # โ print slice containing multiple list items result = my_list[1:3] print(result) # ๐๏ธ ['hadz', '.']
The syntax for list slicing is a_list[start:stop:step]
.
start
index is inclusive and the stop
index is exclusive (up to, but not including).If the start
index is omitted, it is considered to be 0
, if the stop
index
is omitted, the slice goes to the end of the list.
The slice my_list[:2]
starts at index 0
and goes up to, but not including
index 2
.
The slice my_list[-2:]
returns the last 2 elements from the list as negative
indices are used to count backward.
If you need to get a slice somewhere in the middle of the list, specify the
start
and stop
indexes.
my_list = ['bobby', 'hadz', '.', 'com'] start_index = my_list.index('bobby') stop_index = my_list.index('.') # ๐๏ธ ['bobby', 'hadz'] print(my_list[start_index:stop_index])
The list.index()
method returns the index of the first item whose value is
equal to the provided argument.
Note that the stop
index is exclusive (up to, but not including).
Use a for loop if you need to print the items in a list that meet a condition.
my_list = ['bobby', 'hadz', '.', 'com'] for item in my_list: if 'had' in item: print(item) # ๐๏ธ 'hadz'
On each iteration, we
check if the string had
is contained in the list item
and print the matching items.
You can use the same approach to print specific items in a list of lists.
list_of_lists = [['bobby', 'hadz'], ['.', 'com']] print(list_of_lists[0][0]) # ๐๏ธ 'bobby' print(list_of_lists[0][1]) # ๐๏ธ 'hadz'
The first set of square brackets is used to select a sublist in the list.
The second set is used to select a specific item in the sublist.
For example, the slice list_of_lists[0][0]
returns the first element of the
first sublist.
The slice list_of_lists[0][1]
returns the second element of the first sublist.
I've also written an article on how to print a list in columns.
You can learn more about the related topics by checking out the following tutorials: