Saturday, December 28, 2024
Google search engine
HomeLanguagesPython | Convert dictionary to list of tuples

Python | Convert dictionary to list of tuples

Given a dictionary, write a Python program to convert the given dictionary into list of tuples. 

Examples: 

Input: { 'Geeks': 10, 'for': 12, 'Geek': 31 }
Output : [ ('Geeks', 10), ('for', 12), ('Geek', 31) ]

Input: { 'dict': 11, 'to': 22, 'list_of_tup': 33}
Output : [ ('dict', 11), ('to', 22), ('list_of_tup', 33) ]

Below are various methods to convert dictionary to list of tuples. 

Method #1: Using list comprehension 

Python3




# Python code to convert dictionary into list of tuples
 
# Initialization of dictionary
dict = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
# Converting into list of tuple
list = [(k, v) for k, v in dict.items()]
 
# Printing list of tuple
print(list)


Output:

[('Geek', 31), ('for', 12), ('Geeks', 10)]

Time Complexity: O(n), where n is the number of key-value pairs.
Auxiliary Space: O(n)

Method #2 : Using items() 

Python3




# Python code to convert dictionary into list of tuples
 
# Initialization of dictionary
dict = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
# Converting into list of tuple
list = list(dict.items())
 
# Printing list of tuple
print(list)


Output:

[('for', 12), ('Geeks', 10), ('Geek', 31)]

Time complexity: O(n), where n is the number of key-value pairs in the dictionary. This is because the list function takes O(n) time to convert the dictionary into a list of tuples, and the dictionary operations used in the code (e.g., dict.items()) take constant time.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. 

Method #3: Using zip 

Python3




# Python code to convert dictionary into list of tuples
 
# Initialization of dictionary
dict = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
# Using zip
listt = zip(dict.keys(), dict.values())
 
# Converting from zip object to list object
listt = list(listt)
 
# Printing list
print(listt)


Output:

[('Geek', 31), ('Geeks', 10), ('for', 12)]

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.

Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary. 

Method #4: Using iteration 

Python3




# Python code to convert dictionary into list of tuples
 
# Initialization of dictionary
dict = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
# Initialization of empty list
list = []
 
# Iteration
for i in dict:
    k = (i, dict[i])
    list.append(k)
 
# Printing list
print(list)


Output:

[('Geeks', 10), ('for', 12), ('Geek', 31)]

Time complexity: The time complexity of the given code is O(n), where n is the number of key-value pairs in the dictionary. 

Auxiliary space: The auxiliary space used by the code is also O(n), where n is the number of key-value pairs in the dictionary. 

Method #5 : Using collection 

Python3




# Python code to convert dictionary into list of tuples
 
# Initialization of dictionary
import collections
dict = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
# Importing
 
# Converting
list_of_tuple = collections.namedtuple('List', 'name value')
 
lists = list(list_of_tuple(*item) for item in dict.items())
 
# Printing list
print(lists)


Output

[List(name='Geeks', value=10), List(name='for', value=12), List(name='Geek', value=31)]

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

Method #6 : Using map()

Python3




# Python code to convert dictionary into list of tuples
 
# Initialization of dictionary
dict = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
k = list(dict.keys())
l = list(dict.values())
# Converting into list of tuple
list1 = map(lambda a: (k[a], l[a]), range(len(dict)))
 
# Printing list of tuple
print('list is ', [*list1])


Output

list is  [('Geeks', 10), ('for', 12), ('Geek', 31)]

Method 7: Using a loop and the items() method of the dictionary object. 

Approach:

  • Initialize an empty list list1.
  • Iterate through the items of the dictionary using a for loop and the items() method, which returns a list of key-value pairs.
  • For each item in the list, append a tuple of the key and value to the list1.
  • Print the list1 of tuples.

Below is the implementation of the above approach:

Python3




# Initialization of dictionary
dict1 = {'Geeks': 10, 'for': 12, 'Geek': 31}
 
# Initializing an empty list to store the tuples
list1 = []
 
# Iterating through the dictionary and appending tuples to the list
for key, value in dict1.items():
    list1.append((key, value))
 
# Printing the list of tuples
print('list is', list1)


Output

list is [('Geeks', 10), ('for', 12), ('Geek', 31)]

Time complexity: O(n), where n is the number of items in the dictionary. We need to iterate through each item to create the tuples.
Auxiliary space: O(n), where n is the number of items in the dictionary. We need to store n tuples in the list.

Method 8: Using a for loop and the sorted() function

Steps:

  1. Initialize the dictionary.
  2. Sort the keys of the dictionary using the sorted() function.
  3. Use a for loop to iterate over the sorted keys and create a list of tuples where the first element of each tuple is a key from the dictionary and the second element of each tuple is the corresponding value from the dictionary.
  4. Print the list of tuples.

Python3




# Initialization of dictionary
dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }
 
# Sorting the keys of the dictionary
sorted_keys = sorted(dict.keys())
 
# Creating a list of tuples
list_of_tuples = [(key, dict[key]) for key in sorted_keys]
 
# Printing the list of tuples
print('List is', list_of_tuples)


Output

List is [('Geek', 31), ('Geeks', 10), ('for', 12)]

Time complexity: O(nlogn) for sorting the keys of the dictionary using the sorted() function and O(n) for creating the list of tuples using a for loop. Therefore, the overall time complexity is O(nlogn).

Auxiliary space: O(n) for storing the sorted keys in a list and creating the list of tuples.

RELATED ARTICLES

Most Popular

Recent Comments