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

Suggest An Answer

No suggestions Available!