Print specific items in a List in Python

In this article, we will discuss different ways to print specific items in a list in Python.

Table Of Contents

In Python, we can access list items using subscript operator i.e. []. In the subscript operator, we need to pass the index position of element. Indexing starts from 0. Therefore the index position of ith element will be i-1. So, to select 3rd element from list we need to pass the index position as 2.

Let’s see an example,

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23]

# print 3rd element from the list
print(listOfNumbers[2])

Output:

86

In this example, we printed the 3rd element of list i.e. element at index 2.

We can select a range of elements from list using subscript operator i.e.

list[start: end]

It will return list elements from index position start till end-1.

Let’s use this logic to select elements from index 2 to 5 from a list. For that, we need to pass 2 as the start index, and 6 as the end index in the list. Let’s see an example,

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23]

# Print elements from index position 2 till index position 5  
print(listOfNumbers[2 : 6])

Output:

[86, 19, 32, 26]

We printed the elements from index 2 to 5 from the list in Python.

To print first N items of list, pass the start index as empty, and end index as N in the subscript operator of list. Let’s see an example,

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23]

# print first 6 elements from list
print(listOfNumbers[:6])

Output:

[11, 78, 86, 19, 32, 26]

Here we printed the first 6 elements of list.

To print all elements, except first N items of list, pass the start index as N, and end index as empty, in the subscript operator of list. Let’s see an example,

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23]

# print all element of list, after first 5 elements
print( listOfNumbers[5:] )

Output:

[26, 20, 21, 21, 23]

Here we printed all elements of list, except first N elements.

We can iterate over all list elements using a for loop. During iteration, and for each element, check if it satifies a condition or not. If yes, then print that element. Let’s see an example,

listOfNumbers = [11, 78, 86, 19, 32, 26, 20, 21, 21, 23]

# print all elements of list which are divisible by 2
for elem in listOfNumbers:
    if elem % 2 == 0:
        print(elem)

Output:

78
86
32
26
20

Here, we printed all the even numbers from list in Python.

We can use the double subscript operator i.e. [][], to access specific items from list based on index position. For example, to access Mth element from Nth sublist in lists of lists use,

listOfLists[N-1][M-1]

Let’s see an example, where we will print the 4th item from the 3rd sublist in list of lists.

listOfLists = [ [11, 12, 13, 14, 15, 16],
                [21, 22, 23, 24, 25, 26],
                [31, 32, 33, 34, 35, 36] ]

# print elements from 2 dimensional list by index position
print(listOfLists[2][3])

Output:

34

It printed the 4th item from 3rd sub list.

Summary

Today we learned how to print specific items from a list or list of lists in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top