Better way to do this dict comprehesion

Jussi Piitulainen jussi.piitulainen at
Wed Mar 8 07:13:04 EST 2017

Sayth Renshaw writes:

>> To find an unpaired number in linear time with minimal space, try
>> stepping through the list and either adding to a set or removing from
>> it. At the end, your set should contain exactly one element. I'll let
>> you write the actual code :)
>> ChrisA
> ChrisA the way it sounds through the list is like filter with map and
> a lambda. Haven't had a good go
> yet... will see how it goes.

You mean reduce(xor, map(lambda o : {o}. With suitable imports.

I think Chris is suggesting the same thing but in the piecemeal way of
updating a set at each element. You can test for membership, o in res,
and then res.add(o) or res.remove(o) depending on the test.

And you need to say set() to make an empty set, because {} is dict().
Your input sequence is guaranteed non-empty, but it's simply easier to
start with an empty res.

More information about the Python-list mailing list