Friday, September 10, 2021

Python for Beginners: Convert a Dictionary to List of Tuples in Python

We know that a dictionary in python contains key-value pairs. In this article, we will convert a python dictionary to a list of tuples where each tuple contains a key-value pair. 

Convert a dictionary to a list of tuples using a for loop

We can convert a python dictionary to a list of tuples using a for loop by accessing the keys and values of the dictionary one by one. First, we will create tuples using keys and values and then we will append them to a list as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:",myDict)
myList = []
for key in myDict:
    value = myDict[key]
    myTuple = (key, value)
    myList.append(myTuple)
print("The list of tuples is:",myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Convert a dictionary to a list of tuples using the items() method

The items() method, when invoked on a dictionary, returns a dict_items object. The dict_items object contains the key-value pairs of the dictionary. We can convert the dict_items to a list using the list() method as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:", myDict)
items = myDict.items()
myList = list(items)
print("The list of tuples is:", myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Instead of using the list() function, we can use list comprehension to convert the dict_items to a list as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:", myDict)
items = myDict.items()
myList = [item for item in items]
print("The list of tuples is:", myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Convert a dictionary to a list of tuples using the zip() function

We can use the zip() function to create a list of tuples containing the key-value pairs. The zip() function takes iterable objects such as lists as input and merges them into a single iterable object. While merging, the zip() function creates tuples of elements at the same index from each iterable.

For example, If we pass  two lists [1, 2, 3] and [4, 5, 6] to the zip() function, it merges the lists into [(1, 4), (2, 5), (3, 6)]. i.e. the elements at the same index from each iterable are zipped together.

To convert a dictionary to a list of tuples using the zip() function, we will pass the list of keys and the list of values as input to the zip() function.

We can obtain the list of keys using the keys() method. The keys() method, when invoked on a dictionary, returns a dict_keys object containing the keys of the dictionary. We can convert the dict_keys object to a list using the list() function.

Similarly, We can obtain the list of values using the values() method. The values() method, when invoked on a dictionary, returns a dict_values object containing the values of the dictionary. We can convert the dict_values object to a list using the list() function.

We can convert a dictionary to a list of tuples using the keys() and values() method, and zip() function as follows.

myDict = {1: 2, 3: 4, 5: 6, 7: 8}
print("The Dictionary is:", myDict)
keys = list(myDict.keys())
values = list(myDict.values())
myList = list(zip(keys, values))
print("The list of tuples is:", myList)

Output:

The Dictionary is: {1: 2, 3: 4, 5: 6, 7: 8}
The list of tuples is: [(1, 2), (3, 4), (5, 6), (7, 8)]

Conclusion

In this article, we have discussed different ways to convert a dictionary to a List of Tuples in Python. We have used for loop, list comprehension and zip() functions along with dictionary methods like items(), keys(), and values() to convert the dictionary to a list of tuples.

The post Convert a Dictionary to List of Tuples in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...