In this article, we will discuss different ways to convert all key-value pairs of a dictionary to a list of tuples.
Table of Contents
- Convert all pairs of dictionary to list of tuples using items()
- Use zip() to convert all pairs of dictionary to list of tuples
- Use List comprehension to convert all pairs of dictionary to list of tuples
- Use for-loop to convert all pairs of dictionary to list of tuples
- Convert selected pairs of dict to list of tuples in Python
Convert all pairs of dictionary to list of tuples using items()
In Python, dictionary class provides a function items(), which returns an iterable sequence (dict_items) of all key-value pairs of the dictionary. This retuned sequence is a view of the actual key-value pairs in the dictionary. We can use pass this iterable sequence to the list() function to get a list of tuples. For example,
# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, "why": 89, "Hi": 51, "How": 79 } # Convert all key-value pairs of dict to list of tuples list_of_tuples = list(word_freq.items()) print(list_of_tuples)
Output:
[('Hello', 56), ('at', 23), ('test', 43), ('this', 78), ('why', 89), ('Hi', 51), ('How', 79)]
We converted all key-value pairs of a dictionary to a list of tuples.
Use zip() to convert all pairs of dictionary to list of tuples
In Python, the zip() function accepts multiple iterable sequences as argument and returns a merged sequence. In the returned sequence element at ith position is a tuple containing items at ith position of passed sequences.
In dictionary,
Frequently Asked:
- Python : 6 Different ways to create Dictionaries
- Python: Print all key-value pairs of a dictionary
- Python – Initialize a Dictionary with default value
- Create a Dictionary from two Lists in Python
- dict.keys(): Returns an iterable sequence of all keys of dictionary
- dict.values(): Returns an iterable sequence of all values of dictionary
Then we can pass these two sequences (keys and values ) to the zip() function and it will create a list of tuples, where ith tuple in list will contain the ith key and ith value of the dictionary. For example,
# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, "why": 89, "Hi": 51, "How": 79 } # Convert all key-value pairs of dict to list of tuples list_of_tuples = list(zip(word_freq.keys(), word_freq.values())) print(list_of_tuples)
Output:
[('Hello', 56), ('at', 23), ('test', 43), ('this', 78), ('why', 89), ('Hi', 51), ('How', 79)]
We converted all key-value pairs of a dictionary to a list of tuples.
Use List comprehension to convert all pairs of dictionary to list of tuples
dict.items() returns an iterable sequence of all key-value pairs of dictionary. We can iterate over this sequence using a list comprehension and build a list of tuples. In this list of tuple the ith tuple contains the ith key-value pair of dictionary. For example,
# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, "why": 89, "Hi": 51, "How": 79 } # Convert all key-value pairs of dict to list of tuples list_of_tuples = [ (key, value) for key, value in word_freq.items()] print(list_of_tuples)
Output:
[('Hello', 56), ('at', 23), ('test', 43), ('this', 78), ('why', 89), ('Hi', 51), ('How', 79)]
We converted all key-value pairs of a dictionary to a list of tuples.
Use for-loop to convert all pairs of dictionary to list of tuples
We can create an empty list of tuples and then iterate over all-key pairs of dictionary using a for loop and add each key-value pair one by one to the list. For example,
# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, "why": 89, "Hi": 51, "How": 79 } # Create empty list list_of_tuples = [] # Add all key-value pairs of dict to list of tuples for key in word_freq: list_of_tuples.append((key, word_freq[key])) print(list_of_tuples)
Output:
[('Hello', 56), ('at', 23), ('test', 43), ('this', 78), ('why', 89), ('Hi', 51), ('How', 79)]
We converted all key-value pairs of a dictionary to a list of tuples.
Convert selected pairs of dict to list of tuples in Python
In all the previous examples, we converted all key-value pairs of dictionary to a list of tuples. But what if we want to select only few pairs from dictionary based on a condition and convert them to list of tuples.
For example, select key-value pairs from dictionary where value is greater than 50 and convert them to a list,
# Dictionary of string and int word_freq = { "Hello": 56, "at": 23, "test": 43, "this": 78, "why": 89, "Hi": 51, "How": 79 } # Select pairs from dictionary whose value is greater than 50 # Convert these pairs to list of tuples list_of_tuples = [ (key, value) for key, value in word_freq.items() if value > 50] print(list_of_tuples)
Output:
[('Hello', 56), ('this', 78), ('why', 89), ('Hi', 51), ('How', 79)]
We converted only a few pairs of dictionary to a list of tuples.
Summary
We learned about different ways to convert key-value pairs of dictionary to a list of tuples.