Convert List to Dictionary in Python

Changing an inventory to a dictionary in Python will not be that troublesome.

Let’s take a look at a number of methods to transform an inventory right into a dictionary.

Introduction

Earlier than we get to the tutorial, let’s have a look at an instance of changing an inventory to a dictionary. We may have an inventory of tuples (every tuple consists of two components) or only a record of components. We take each inputs and convert them to dictionaries.

Examples:

Within the following instance, we took an inventory of tuples and turned it right into a dictionary. We took the primary component of every tuple as the important thing and the second component because the dictionary worth.

Enter: list_one = [(‘a’, ‘A’), (‘b’, ‘B’), (‘c’, ‘C’)]

Exit: {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’}

Within the following instance, we took an inventory of components and transformed it right into a dictionary with different components as key and worth. We cross null to the important thing if there is no such thing as a component left for it as a key (if the record has an odd variety of components).

Enter: list_one = [‘a’, ‘A’, ‘b’, ‘B’, ‘c’, ‘C’, ‘d’]

Exit:{‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: None}

Now we have seen the aim of the tutorial. And we are going to focus on each examples in several methods. Let’s begin with the primary instance.

Listing of Tuples – Dictionary

Let’s have a look at the right way to convert an inventory of tuples right into a dictionary. You may attempt writing the code utilizing the next steps.

  • Initialize the record of tuples with dummy knowledge as given within the examples above (be certain every tuple within the record has precisely two components).
  • Move the record of tuples to dict methodology and shops the end in a brand new variable.
  • That is it, we turned an inventory of tuples right into a dictionary with a single line of code.
# record of tuples
list_of_tuples = [('a', 'A'), ('b', 'B'), ('c', 'C')]

# changing to dictionary
list_of_tuples_dict = dict(list_of_tuples)

# printing the outcome dict
print(list_of_tuples_dict)

You may take a look at the above program output by operating it. You’re going to get the outcome as we see within the examples.

Listing – Dictionary

We have seen the right way to flip an inventory of tuples right into a dictionary, and it is very easy in Python. On this part, we’ll see the right way to flip a easy record right into a dictionary.

See the second instance from the primary a part of the tutorial for extra readability.

Comply with the steps under to write down the code for the second instance.

Now we have entered the default worth as No for the component that may don’t have any worth (an inventory with an odd variety of components). For that we have to use a way known as zip_longest from the module itertools.

  • Import the module itertools and initialize an inventory with an odd variety of components from the examples.
  • Convert the record to an iterable to keep away from repetition key And worth {couples} within the zip_longest methodology.
  • Now cross the iterable to the strategy zip_longest And filling worth if No. It returns a zipper object.
    • Now we have to cross iterable twice as a result of it will get key and worth from the 2 iterables. In our case, each the keys and the values ​​are in the identical iteration. So now we have to cross it twice earlier than the worth will get populated.
  • Convert the item to the Python dictionary utilizing dict methodology.
  • Print the outcome.
# importing the module
import itertools

# plain record with odd variety of components
plain_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd']

# changing it to iterable to keep away from repetition
plain_list_iter = iter(plain_list)

# changing the plain_list to dict
plain_list_dict_object = itertools.zip_longest(plain_list_iter, plain_list_iter, fillvalue=None)

# convert the zip_longest object to dict utilizing `dict`
plain_list_dict = dict(plain_list_dict_object)

# print it
print(plain_list_dict)

You need to use the filling worth no matter you need. Strive various things with it. And run the code to see if we get the precise output as said within the instance or not.

Conclusion

I hope you loved turning the record right into a dictionary. Let’s meet within the subsequent tutorial.

Subsequent, learn to flatten the record in Python.

Have enjoyable coding 🙂

Leave a Comment

porno izle altyazılı porno porno