In this article, we will discuss different ways to print specific items in a list in Python.
Table Of Contents
- Print a specific item from list based on index position
- Print a range of items from list based on index positions
- print first N elements of list
- Print all elements of list except first N elements
- Print specific list items that satisfy a condition
- Print sepcific items from list of lists in Python
- Summary
Print a specific item from list based on index position
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:
Frequently Asked:
86
In this example, we printed the 3rd element of list i.e. element at index 2.
Print a range of items from list based on index positions
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,
Latest Python - Video Tutorial
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.
print first N elements of list
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.
Print all elements of list except first N elements
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.
Print specific list items that satisfy a condition
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.
Print sepcific items from list of lists 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.
Latest Video Tutorials