Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']
like image 433
M Hammad Awan Avatar asked Jan 20 '26 10:01

M Hammad Awan


2 Answers

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']
like image 182
Sociopath Avatar answered Jan 22 '26 23:01

Sociopath


for index,i in enumerate(li):
    index = index+1
    if index % 3 == 0: print(i)
like image 38
mirhossein Avatar answered Jan 23 '26 00:01

mirhossein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!