Home > Blockchain >  Operations on arrays in Python
Operations on arrays in Python

Time:01-14

I have a simple question. For example, how can I get the odd numbers in a= ['p','y','t','h','o','n'] array. output to me: It should be ['y','h','n']. Thank you.

CodePudding user response:

you can use slicing :

output = a[1::2]

CodePudding user response:

a = ['p','y','t','h','o','n']

only consider a character where the index is odd

b = [item for idx, item in enumerate(a) if idx % 2 != 0]

print(b) // ['y', 'h', 'n']

  •  Tags:  
  • Related