Can anyone explain to me, what exactly this code snippet is doing?
chained_country_list = set(itertools.chain.from_iterable(country_and_countrycodes)) & set(
It's hard to tell what does your code do without knowing the type and value of the objects passed to the functions. However, the function chain.from_iterable
tries to create a flattened iterable off of country_and_countrycodes
which presumably should be a nested iterables like a nested list. At next step the set
function creates a set from the flattened result in order to be and-able with set(all_countries)
.
Now as a more Pythonic alternative to the following part:
set(itertools.chain.from_iterable(country_and_countrycodes))
You could just pass the iterables to set().union()
function in order to create a union set of unique items at once.
Example:
In [2]: set().union(*[[1, 3], [5, 6], [3, 5]])
Out[2]: {1, 3, 5, 6}
So, you can change that code to the following:
set().union(*country_and_countrycodes) & set(all_countries)
# Or
# set().union(*country_and_countrycodes).intersection(all_countries)
Let's break down each significant element of the code:
itertools.chain.from_iterable:
Basically, this is used to flatten a nested list, like this:
l = [[0], [1, 2], [2], [3, 6], [4], [5, 10]]
list(itertools.chain.from_iterable(l))
Output:
[0, 1, 2, 2, 3, 6, 4, 5, 10]
&
operator between two sets:
Consider the follow example of sets a and b.
a = {1, 2, 3}
b = {2, 3, 4}
a & b
Output:
{2, 3}
So basically it gets the common elements between two sets. Here they're 2 and 3.
The code as a whole:
Let's say:
country_and_countrycodes = [('United States', 'US'), ('China', 'CH')]
all_countries = ['United States', 'Mongolia', 'Togo']
Now, the first part is:
set(itertools.chain.from_iterable(country_and_countrycodes))
which gives us:
{'CH', 'China', 'US', 'United States'}
So, it just gets us a flat set from the tuples.
Then, the second part is:
set(itertools.chain.from_iterable(country_and_countrycodes)) & set(all_countries)
which gives us:
{'United States'}
Basically, what we did was:
{'CH', 'China', 'US', 'United States'} & {'United States', 'Mongolia', 'Togo'}
Since the only common element here is 'United States'
, that's what we got as the output.