What is the name of the dictionary method that returns a list of pairs represented as tuples?

To make a tuple more readable, it’s often necessary to convert it to a dictionary object. Data models that would be used to process data in Python include lists and dictionaries. In contrast to dictionaries, Python lists are an ordered succession of items. 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 Python, the dict() function makes a dictionary. A dictionary is an unordered, changeable, and indexable iterable collection object. The intrinsic function tuple() in Python is used to build a tuple. A tuple is a form of sequential manner that cannot be modified. Converting a type of data to another is a typical task, and we’ll explore how to do so in this article.

Here, we will use strategies to make a dictionary out of a list of tuples in this article.

Example 1: Convert a List of Tuples to dict Using the dict Function in Python

The dict() function can be used to create a dictionary object. The dictionary is returned by the dict() method, which accepts a list of tuples as a parameter. A key-value pair is contained in each tuple.

Below, we have assigned a list of tuples to a variable “Tuple_List.” This list of tuples contains the integer and the string values. The list of tuples is printed with the print statement. Then, we have created another variable, “Tuple_dict,” to which the dict() function is assigned. The dict() function has key values as “x” and “y” for each tuple. Within the dict() function, we have applied a dictionary comprehension method which is used for converting one dictionary into another. Elements from the source dictionary are dynamically included in the new dictionary throughout this conversion, and every element is altered as needed.

Tuple_List = ((1,"Monday"),(2,"Tuesday"),(3,"Wednesday"),(4,"Thursday"),(4,"Friday"),(5,"Saturday"),(7,"Sunday"),)

print("List:",Tuple_List)

Tuple_Dict = dict((j, i) for i, j in Tuple_List)

print("Dict:",Tuple_Dict)

The output shows the list of tuples and the dictionary as follows.

Example 2: Convert a List of Tuples to dict Using the map Function in Python

To convert a tuple to the dictionary, utilize the map() function with the dict() function and reversed function together. A map object is returned as an iterator by the map() method.

Firstly, we have declared a variable “tuplist” and initialized it with the tuple of integer values and string values. That will be printed as we have called the print function over it. Then, the variable “Dict” is created where the dict function is invoked. Inside the dict function, we have used the map to which we have passed a reversed function and a tuple list as an argument. The map function will return the key-value pair from the list of tuples.

tuplist = ((100, "Ten"),(100, "Hundred"), (1000, "Thousand"))

print("List:",tuplist)

Dict = dict(map(reversed, tuplist))

print("Dict:",Dict)

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. You can use this method or the dictionary comprehension method described above, depending on your needs.

Example 3: Convert a List of Tuples to dict Using setdefault Function in Python

The setdefault() method requires two parameters: a key and a dictionary value. If a key is not found, the setdefault() function generates a new key with a def value.

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. The dict.setdefault function is called and takes a parameter “a” as a key value and adds the values to the second parameter. Then, the append method is used to add the values in the dictionary. 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. Now, the print function has the convert function in which we have passed the tuple variable and dict variable to be printed.

def convert(tuple, dict):

for a, b in myTuple:

dict.setdefault(a, []).append(b)

return dict

myTuple= [("Rose", 17), ("jasmine", 18), ("sofia", 15), ("Bella", 16)]

MyDictionary = {}

print(convert(myTuple, MyDictionary))

It returns a dictionary with a key element at the first of a tuple and a value included within a list, as shown.

Example 4: Convert a List of Tuples to dict Using the fromkeys Function in Python

The fromkeys() function is another option for converting a tuple list to a Python dictionary.

Two variables are represented by the names “color_keys” and “color_value.” The variable “color_keys” has the list of tuples, and the color_value has the values to be set for the keys in the above list of tuples. We can achieve this by the fromkeys function, which takes these two declared variables inside it as an argument.

The fromkeys function is used within the dict function which is assigned to the variable “dict_color”. The dict function provided a dictionary containing a list of tuples with the value.

color_keys = ['Indigo', 'Green', 'Yellow', 'Pink']

color_value = 'color'

color_dict = dict.fromkeys(color_keys, color_value)

print(color_dict)

As shown, the output has the value set with the key item in the below python dictionary.

Example 5: Convert a List of Tuples to dict Using the zip Function in Python

Using the dict() and zip() functions together is one of the simplest strategies for converting a list of tuples to a dictionary. We are using the dict(), and zip() functions to make a dictionary out of two sequences. For both dict and zip, dict(zip(keys, values)) requires a one-time global lookup.

The two lists are created as “player_name” and “player_score,” which will be paired up in the dictionary by using the zip function. The zip function is passed with the two lists, which will zip them together. The zip function is called inside the dict function. Then, we have assigned the dict function operation to a variable “players.”

players_name = ['Smith', 'Ravi', 'Hashim', 'Babar']

players_score = [67, 84, 50, 100]

players = dict(zip(players_name, players_score))

print(players)

The two lists are zipped together and returned in a dictionary format as follows.

Conclusion

Python includes the list and dictionary, which are the most reliable data structures. The most popular technique you have ever encountered in Python development is converting from one format to another. Thus, we have shown you the different approaches with the python example programs, which convert the list of tuples to a dictionary.

Postingan terbaru

LIHAT SEMUA