简体   繁体   中英

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']

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']
for index,i in enumerate(li):
    index = index+1
    if index % 3 == 0: print(i)

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM