Convert Dictionary to List of Tuples Pairs

Python: Convert Dictionary to List of Tuples/ Pairs

Dictionaries are Python’s implementation of an associative list, which may be a arrangement . A dictionary may be a collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.

Examples:

Input:

dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}

Output:

[('This', 100), ('is', 200), ('BTechGeeks', 300)]

Convert Dictionary to List of Tuples/ Pairs

There are several ways to convert dictionary to list of tuples/pairs some of them are:

Method #1:Using zip() function

The zip function combines the parameters passed to it. So we pass the dictionary’s keys and values as parameters to the zip function, and the result is passed to a list function. The key-value pair is converted into a list of tuples.

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# converting dictionary to list of tuples
listtuple = list(zip(dictionary.keys(), dictionary.values()))
# print list of tuples
print(listtuple)

Output:

[('This', 100), ('is', 200), ('BTechGeeks', 300)]

Method #2:Using items()

The dictionary class in Python includes the items() function, which returns an iterable sequence (dict items) of all key-value pairs in the dictionary. This retuned sequence is a representation of the dictionary’s actual key-value pairs. To get a list of tuples, we can pass this iterable sequence to the list() function.

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# converting dictionary to list of tuples
listtuple = list(dictionary.items())
# print list of tuples
print(listtuple)

Output:

[('This', 100), ('is', 200), ('BTechGeeks', 300)]

Method #3:Using List Comprehension

dict.items() returns an iterable sequence of all dictionary key-value pairs. We may use a list comprehension to iterate over this sequence and create a list of tuples. The ith tuple in this list of tuples represents the ith dictionary key-value pair.

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# converting dictionary to list of tuples using list comprehension
listtuple = [(key, value) for key, value in dictionary.items()]
# print list of tuples
print(listtuple)

Output:

[('This', 100), ('is', 200), ('BTechGeeks', 300)]

Method #4:Using for loop and append() functions

We can start with an empty list of tuples and then use a for loop to iterate over all key-value pairs in the dictionary, adding each key-value pair to the list one by one.

Below is the implementation:

# given dictionary
dictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}
# creating empty list
listtuple = []
# using for loop to traverse the dictionary
for key in dictionary:
    # append key and value to list as a tuple
    listtuple.append((key, dictionary[key]))

# print list of tuples
print(listtuple)

Output:

[('This', 100), ('is', 200), ('BTechGeeks', 300)]

Related Programs: