How to print the first 5 elements of list python?
To generate the first 5 elements from a list in Python, use the isclice function as follows:
BY Best Interview Question ON 11 Jun 2020
Example
From itertools import islice
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
iterator = islice(l, 5)
for item in iterator:
print item
Output
1
2
3
4
5