How do I print only odd numbers from Python list?

Asked 2 years ago, Updated 2 years ago, 14 views

[1,3,3,2,1] What should I do to make it so that 2 is printed out?

python

2022-09-21 10:21

1 Answers

>>> from collections import Counter
>>> l = [ 1,3,3,2,1]
>>> counter = Counter(l)
>>> counter
Counter({1: 2, 3: 2, 2: 1})
>>> odd_counts = [ k for k, v in counter.items() if v%2 ]
>>> odd_counts
[2]


2022-09-21 10:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.