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,
- 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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.