M Hammad Awan
M Hammad Awan

Reputation: 39

How to get all numbers from list which are located at multiple of 3 index

How to get all numbers from list which are located at multiple of 3 index for example

li=['ab','ha','kj','kr','op','io']

i need

['kj','io']

Upvotes: 3

Views: 1089

Answers (2)

mirhossein
mirhossein

Reputation: 737

for index,i in enumerate(li):
    index = index+1
    if index % 3 == 0: print(i)

Upvotes: 1

Sociopath
Sociopath

Reputation: 13401

Use slicing on list where [2::3] means start from 2nd index(indices start from 0 in python) and get every 3rd element

print(li[2::3])

Output:

['kj','io']

Upvotes: 4

Related Questions