HomeLinuxConvert a Record of Tuples to a Dictionary in Python

Convert a Record of Tuples to a Dictionary in Python


Changing a listing right into a Python Dictionary is a typical job typically carried out for both the higher readability of the info or to characterize sure knowledge within the type of key-value pairs. Nevertheless, when you may have a listing of tuples, new programmers change into confused about its conversion right into a dictionary. There are totally different strategies that let you convert your record of tuples right into a Python Dictionary simply, and this information goes to cowl them.

The weblog will cowl the next:

Technique 1: Use the dict() Technique to Convert a Record of Tuples to a Dict

The dict() constructor can be utilized to generate Dictionary objects from a listing of tuples in Python. Nevertheless, you can’t simply move within the record of tuples. Relatively, it’s essential use record comprehension to fetch the 2 components of the tuples and move them into the dict() constructor, because it takes two arguments.

To display the working of the dict() technique to create a dict from a listing of tuples, take the next code:

listVar = ((1,“Apple”),(2,“Banana”),(3,“Peach”),(4,“Orange”))
print(“Preliminary Record: “,listVar)
dictVar =  dict((i,j) for i,j in listVar)
print(“Resultant Dict: “,dictVar)

When this code is executed, it produces the next output on the terminal:

From the output, you’ll be able to simply observe that the record of sorts was efficiently transformed right into a Python Dictionary.

Technique 2: Use the map() Technique With the dict() Constructor

One other manner of changing a listing of tuples right into a Python dictionary is through the use of the map() technique inside the dict() constructor and passing the tuples record into the map() technique. Nevertheless, to have the ability to convert the record right into a dict, the map() technique would require two arguments. Subsequently, you merely present “reversed” as the primary argument, which can reverse the position of the weather within the tuple.

To display the working of the map() technique, use the next code snippet:

listVar = ((1,“Apple”),(2,“Banana”),(3,“Peach”),(4,“Orange”))
print(“Preliminary Record: “,listVar)
dictVar =  dict(map(reversed,listVar))
print(“Resultant Dict: “,dictVar)

When this code is executed, it produces the next outcome on the terminal:

From the output, you’ll be able to see that the record of tuples has been transformed right into a dictionary efficiently. Nevertheless, the weather of the tuple have been reversed.

Technique 3: Use the zip() Technique With the dict() Constructor

If the values of the tuple are positioned inside separate lists, the person can use the zip() technique inside the dict() constructor to create a Python dictionary. To display this, take the next code snippet:

listVar1 =[“Apple”,“Banana”,“Peach”,“Orange”]
listVar2= [1,2,3,4]
dictVar =  dict(zip(listVar2,listVar1))
print(“Resultant Dict: “,dictVar)

When this code snippet is executed, it can produce the next outcomes on the terminal:

As you’ll be able to see, the weather from the 2 totally different lists have been merged to type a Python Dictionary.

Conclusion

An inventory of sorts will be transformed right into a Python dictionary through the use of the dict() constructor with both the record comprehension method or through the use of the map() technique inside the dict() constructor. Furthermore, if the weather to be fashioned right into a Python dict are positioned in separate lists, then the person can make the most of the zip() technique inside the dict() constructor.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments