Instead of processing items based on its value, I need to run a function based on the item's position. This is an example of filtering based on the content of the list.
only_words = filter(str.isalpha, my_list)
I want to create a slice of the list that contains only items in the even position, that is:
new_list = []
pos = 0
for item in my_list:
if pos % 2 == 0:
new_list.append(item)
This is way too ugly. Better suggestions ?
You can use this: my_list[::2]
(for odd — my_list[1::2]
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With