The fromkeys() function is another option for converting a tuple list to a Python dictionary. It returns a dictionary with a key element at the first of a tuple and a value included within a list, as shown.Įxample 4: Convert a List of Tuples to dict Using the fromkeys Function in Python Print (convert (myTuple, MyDictionary ) ) Now, the print function has the convert function in which we have passed the tuple variable and dict variable to be printed. After that, we initialized the list of tuples and assigned it to the variable “myTuple.” The variable “MyDictionary” is created for the dictionary items to be added from the tuple of the list.
Then, the append method is used to add the values in the dictionary.
The tdefault function is called and takes a parameter “a” as a key value and adds the values to the second parameter. Here, we have defined a function “convert” with the keyword “def.” The “convert” function has two types “tuple” and “dict.” Then, we have for loop for the iteration over the items in the tuple list. If a key is not found, the setdefault() function generates a new key with a def value. The setdefault() method requires two parameters: a key and a dictionary value. You can use this method or the dictionary comprehension method described above, depending on your needs.Įxample 3: Convert a List of Tuples to dict Using setdefault Function in Python We have the dictionary as an output, but the first tuple element is now a value, and the tuple second element is now the dictionary’s key. The map function will return the key-value pair from the list of tuples. Inside the dict function, we have used the map to which we have passed a reversed function and a tuple list as an argument. Then, the variable “Dict” is created where the dict function is invoked. That will be printed as we have called the print function over it. A map object is returned as an iterator by the map() method.įirstly, we have declared a variable “tuplist” and initialized it with the tuple of integer values and string values. To convert a tuple to the dictionary, utilize the map() function with the dict() function and reversed function together. The output shows the list of tuples and the dictionary as follows.Įxample 2: Convert a List of Tuples to dict Using the map Function in Python Tuple_Dict = dict ( (j, i ) for i, j in Tuple_List ) Elements from the source dictionary are dynamically included in the new dictionary throughout this conversion, and every element is altered as needed. Within the dict() function, we have applied a dictionary comprehension method which is used for converting one dictionary into another. The dict() function has key values as “x” and “y” for each tuple. Then, we have created another variable, “Tuple_dict,” to which the dict() function is assigned. The list of tuples is printed with the print statement. A key-value pair is contained in each tuple.īelow, we have assigned a list of tuples to a variable “Tuple_List.” This list of tuples contains the integer and the string values. The dictionary is returned by the dict() method, which accepts a list of tuples as a parameter. The dict() function can be used to create a dictionary object. Example 1: Convert a List of Tuples to dict Using the dict Function in Python Here, we will use strategies to make a dictionary out of a list of tuples in this article.
Converting a type of data to another is a typical task, and we’ll explore how to do so in this article. A tuple is a form of sequential manner that cannot be modified. The intrinsic function tuple() in Python is used to build a tuple. A dictionary is an unordered, changeable, and indexable iterable collection object. In Python, the dict() function makes a dictionary. The elements in the list could be found using an index (dependent on their position), whereas the objects in the dictionary could be found using keys rather than their position. In contrast to dictionaries, Python lists are an ordered succession of items. Data models that would be used to process data in Python include lists and dictionaries. To make a tuple more readable, it’s often necessary to convert it to a dictionary object.