1.

How to print the first 5 elements of list python?

Answer»

To GENERATE the first 5 elements from a list in Python, use the isclice function as follows: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



Discussion

No Comment Found