3) j. Conversion between data-structures

We’re now familiar with different popular collections in Python – lists, dictionaries and sets – but how do you convert from one to the other on demand?

Here’s a list of methods to convert from one collection to another. All the 8 ways are presented together on this page. Try to spot compare them with each other. What is similar and different between these different methods. This is a do-it-yourself exercise in which you’ll form your own neuron connections by comparing these conversion methods. This will also help you in understanding what is going on and recalling them these later on when you sit down to code.

Hint: If you’re converting TO datatype X, then you will use a function of X. For example if you’re converting to a List then you need to use the list() function. See if you can spot this in the examples below or check out the image at the bottom of the page!

1. Converting from list → dict

The simplest way is to use the ‘map’ function. We’re assuming each element of the list includes both ordinary text and the index. For example, an element is “27. This is a string with an index”. The resulting dictionary should transform it to {“27” : “This is a string with an index”}. In other words, we’re also assuming you’ve separated the index and content with some kind of mark (it could be ‘:’, or ‘.’ or anything). Just apply the following method to create the dictionary:

1
2
Output
{'1': ' First', '2': ' Second', '3': ' Third', '4': ' Fourth'}

Alternatively, if your list contains elements that need to be paired up to create a map then you could do as follows. For example, your list has the elements [“Batman”, “Wayne”, “Superman”, “Clark”] and the resulting dictionary must be {“Batman” : “Wayne”, “Superman”, “Clark”}.

For achieving this you could use the following code:

1
2
Output
{'Batman': 'Wayne', 'Superman': 'Clark'}

What really happens in this code is this:

[::2] = start from 0th element until the end
Keep picking alternate element (“second element”)

[1::2] = start from 1st element until the end
Keep picking alternate element (“second element”)




2. Converting from dict → list (direct method of using values)

How do you achieve this? Just use a for-loop in dictionary_name.items() – append the key and value to the list.

1
2
Output
['Wayne is Batman', 'Clark is Superman', 'Peter is Spiderman']




3. Converting from list → set

This is easy – just type set(listName). Your set is ready – just don’t expect the order of the elements to always turn out the same. Sets are unordered.

1
2
Output:
['Wayne is Batman', 'Clark is Superman', 'Peter is Spiderman']




4. Converting from set → list

This is equally easy and the converse of the previous conversion – just type list(setName). Your list is ready – but again, don’t expect the order of the elements to always turn out the same. Sets are unordered.

1
2
Output:
['Michael Scott', 'Dwight Schrute', 'Jim Halpert', 'Pam Beesly', 'Ryan Howard', 'Andy Bernard', 'Robert California']




5. Converting from set → dict

The easiest way to achieve this is to first convert from set to list, and then from list to dictionary. However, note that in most cases you will never encounter this situation. The problem of converting from sets to dict is that the sets are unordered.

What does that mean? It means that the elements of the sets will be accessed in any order. So when you’re converting to a dictionary, which is ordered, then you would end up making key-value pairs that are not what you intended. Here’s an example that illustrates why this conversion is discouraged:

1
2
Output:
{'Batman': 'Superman', 'Spiderman': 'Wayne', 'Clark': 'Peter'}




6. Converting from dict → set

The easiest way to achieve this is to convert from dictionary to list, then list to set.

1
2
Output:
{'Batman', 'Peter', 'Clark', 'Superman', 'Wayne', 'Spiderman'}

7. Converting from tuple → list

The easiest way to achieve this is to convert from tupple to list is to use the list() function.

1
2
Output:
['Batman', 'Superman', 'Spiderman', 'Wayne', 'Clark', 'Peter']

8. Converting from list → tupple

The easiest way to achieve this is to convert from list to tuple is to use the tupple() function.

1
2
Output:
('Batman', 'Superman', 'Spiderman', 'Wayne', 'Clark', 'Peter')

This should give you enough to get a hang of these conversions between these data-structures. It gets easier with practice. So don’t worry if this seems daunting right now.

Here is an image that shows the summary of what we’ve covered.