How to Print specific items in a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Print specific items in a List in Python
  2. Print multiple specific list items
  3. Printing a slice in the middle of the list
  4. Printing the items that meet a given condition
  5. Printing specific items in a list of lists

# 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 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', '.']

print specific items in list

The code for this article is available on GitHub

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'

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.

# Print multiple specific list items

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

main.py
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', '.']

print multiple specific list items

The code for this article is available on GitHub

The syntax for list slicing is a_list[start:stop:step].

The 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.

# Printing a slice in the middle of the list

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])

print slice in middle of list

The code for this article is available on GitHub

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).

# Printing the items that meet a given condition

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'

print list items that meet given condition

The code for this article is available on GitHub

On each iteration, we check if the string had is contained in the list item and print the matching items.

# Printing specific items in a list of lists

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'

print specific items in list of lists

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev