 Hi folks, I need help with the following python list comprehension code

`return [
x for x in nums
if x % 2 != Counter([n % 2 for n in nums]).most_common()
] `

What’s the Counter function? It isn’t defined, or did you import it from a library? If u did then please tell which library.

But I guess this might satisfy you: This code belongs to some function which will return a list, the list is comprehended, the x belongs to the for loop, which loops through an iterable nums. In each iteration the code after nums is executed, which checks if the remainder obtained by dividing x by 2 is not equal to Counter([n % 2 for n in nums]).most_common(). If it is equal then x will not be appended to the list.(i.e, x will only be appended to the list if the if block evaluates to true)

1 Like

the whole program is

from collections import Counter
def find_parity_outliers(nums):
return [
x for x in nums
if x % 2 != Counter([n % 2 for n in nums]).most_common()
]
print(find_parity_outliers([1, 2, 3, 4, 6]))

I appreciate your help. but could please write list comp in simple statments outside list comp.
thanks

Sure. I don’t know what the counter function does, but it returns some value. In the return array(the list which is being returned) of find_parity_outliers function, x states to append x. In comprehension, there’s a feature where you can compare a value in an if block, if the block evaluates to True then the counter variable (the variable which is storing the value of the current loop/iteration, i.e, in simple words it’s x over here) will be comprehended to the array(it will be added to the array). However, if the if block evaluates to False, the counter variable(x) won’t be appended to the array.

1 Like

Starting from the inside and working out:

n % 2 returns 0 for even numbers and 1 for odd numbers. So the list
comprehension takes a list of numbers and changes it to a list of 0 and
1 (even and odd).

The Counter class counts how many times each element it sees occurs. So
it counts how many even and odd number in the list.

The most_common() method returns a list of the most common (key,count)
from the counter. Then it is subscripted  to get the first item in
the list, then a second time  to get the most common key, which
will be 0 or 1.

So the code Counter(...) returns 0 if there are more even
numbers, and 1 if there are more odd numbers.

The outer list comprehension:

[x for x in nums if x % 2 != ...]

returns a list of the numbers in nums which are not the same as the
most common odd or even.

So if you have more odd numbers:

nums = [1, 3, 2, 5, 7, 9, 4]

the list comp returns even numbers [2, 4]. If you have more even
numbers:

nums = [8, 6, 4, 5, 2, 1, 8]

the list comp returns odd numbers [5, 1].

But note that this is very inefficient. The outer list comprehension
calls Counter and the inner list comprehension for every element in the
list. So if you have 100 numbers, it will call Counter() one hundred
times, to get the same result each time. Each time you call Counter(),
it has to look at each value in the nums list.

So if you have 100 numbers, the Python interpreter has to loop over the
100 numbers in the list 100 times, or ten thousand times. If you have
1000 numbers, the interpreter has to loop 1000x1000 = 1000000 (a
million) times.

You can test this for yourself. Python should be able to iterate over a
million numbers in a second or less. If you do:

temp = list(range(1000000))

even on a slow computer, Python will finish in an eye-blink.

But now try using your double list comprehension on a million numbers:

range(1000000)

If I am right, the interpreter will need to do one million loops of a
million items each time, or 1000000000000 iterations, which even in a
super-fast PC will take hours, or days, or weeks.

You can interrupt the code by typing Ctrl-C in the interpreter.

A better way is to pull the Counter(…) call out of the loop, so it is
only performed once:

parity = Counter([n % 2 for n in nums]).most_common()
return [x for x in nums if x% 2 != parity]
1 Like