avatar Bite 323. Iterables intersection

In this bite we are going to work on coding a function intersection() that searches the common elements across its input. More specifically:

- Input to the function will be one, two or more objects which can be iterated on (e.g., list, tuple, str)

- The function's output is a set containing the intersection across input iterables, or an empty set if no common elements are found


- Filter out inputs which are None or empty

- If after the filter you end up with just one iterable, the output is a set with the unique elements in that iterable (see last example below)

Here is how it works in the Python interpreter's Read Execute Print Loop (REPL):

>>> from intersection import intersection
>>> intersection({1,2,3}, {2,3,4}, {3,4})
>>> intersection([1,2,3,"1"], {1,-1}, {})
>>> intersection(None, "this is a string")
{' ', 'a', 'g', 'h', 'i', 'n', 'r', 's', 't'}

Note: the template provides a hint to use a standard library module that can help you, but it is not mandatory for solving this bite (i.e., there are at least two ways to code intersection())

Login and get coding
go back Beginner level
Bitecoin 2X

111 out of 113 users completed this Bite.
Will you be Pythonista #112 to crack this Bite?
Resolution time: ~58 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 4.25 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help