We use Python 3.6

go back Level: Intermediate (img: IM / score: 3) level Bite 33. Transpose a data structure

30 out of 32 users completed this Bite ...
Will you be Pythonista #31 to crack this Bite?
Average Bite difficulty 1-10 rating: 3.5
» You can do it! 😌

Sometimes you need to restructure a nested data structure. For example you can convert a dict in a list of (key, value) tuples via dict.items().

In this Bite a real world scenario where we wanted to plot some data from a Counter dict.

For plots you typically need 2 lists: X + Y axis or labels + values. So we needed an easy way to transpose data structures.

Complete the transpose function to do just that. It has to work for both dicts and lists of (named)tuples. Examples given in the docstring. See also the TESTS tab. Have fun!

Github login button