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.
Asked
Active
Viewed 45 times
-3
-
There is no odd number in `a`. Also, what have you tried? – Jan 13 '22 at 19:12
-
So you want something like `print(['p','y','t','h','o','n'][1::2])`? – Matthias Jan 13 '22 at 19:13
-
you can use the slice notation to get it. [understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Copperfield Jan 13 '22 at 19:14
2 Answers
0
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']

Joseph Mukorivo
- 7
- 3