Print specific items in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Print specific items in a List in Python #

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.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] # ✅ print first item in list first = my_list[0] print(first) # 👉️ 'bobby' # ✅ print last item in list last = my_list[-1] print(last) # 👉️ 'com' # ✅ print slice containing multiple list items result = my_list[1:3] print(result) # 👉️ ['hadz', '.'] # ✅ print list items that meet condition for item in my_list: if 'had' in item: print(item) # 👉️ 'hadz' # ----------------------------------------- # ✅ print specific items in list of lists list_of_lists = [['bobby', 'hadz'], ['.', 'com']] print(list_of_lists[0][0]) # 👉️ 'bobby' print(list_of_lists[0][1]) # 👉️ 'hadz'

If you need to print a single list item, access the list at the specific index.

main.py
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.

main.py
my_list = ['bobby', 'hadz', '.', 'com'] variable = 'bobby' index = my_list.index(variable) print(index) # 👉️ 0 print(my_list[index]) # 👉️ 'bobby'

You list slicing if you need to print multiple items in the list.

main.py
# ✅ print first 2 items in list print(my_list[:2]) # 👉️ ['bobby', 'hadz'] # ✅ print last 2 items in 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 my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

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.

If you need to get a slice somewhere in the middle of the list, specify the start and stop indexes.

main.py
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.

main.py
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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.