Python – Append Dictionary Keys and Values ( In order ) in dictionary
Last Updated :
30 Mar, 2023
Given a dictionary, perform append of keys followed by values in list.
Input : test_dict = {“Gfg” : 1, “is” : 2, “Best” : 3}
Output : [‘Gfg’, ‘is’, ‘Best’, 1, 2, 3]
Explanation : All the keys before all the values in list.
Input : test_dict = {“Gfg” : 1, “Best” : 3}
Output : [‘Gfg’, ‘Best’, 1, 3]
Explanation : All the keys before all the values in list.
Method #1 : Using list() + keys() + values()
This is one of the ways in which this task can be performed. In this, we extract keys and values using keys() and values(), convert then to list using list() and perform append in order.
Python3
test_dict = { "Gfg" : 1 , "is" : 3 , "Best" : 2 }
print ( "The original dictionary is : " + str (test_dict))
res = list (test_dict.keys()) + list (test_dict.values())
print ( "The ordered keys and values : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : ['Gfg', 'is', 'Best', 1, 3, 2]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using chain() + keys() + values()
This is one of the ways in which this task can be performed. In this, we bind keys with values together in order using chain().
Python3
from itertools import chain
test_dict = { "Gfg" : 1 , "is" : 3 , "Best" : 2 }
print ( "The original dictionary is : " + str (test_dict))
res = list (chain(test_dict.keys(), test_dict.values()))
print ( "The ordered keys and values : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : ['Gfg', 'is', 'Best', 1, 3, 2]
Time complexity: O(n), where n is the size of the dictionary.
Auxiliary space: O(n), as we are creating a list of size 2n to store the concatenated keys and values.
Method #3 : Using list() +keys() + values() + extend()
Python3
test_dict = { "Gfg" : 1 , "is" : 3 , "Best" : 2 }
print ( "The original dictionary is : " + str (test_dict))
a = list (test_dict.keys())
b = list (test_dict.values())
a.extend(b)
res = a
print ( "The ordered keys and values : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : ['Gfg', 'is', 'Best', 1, 3, 2]
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 zip() function and list comprehension
This program initializes a dictionary and prints it. It then uses a zip() function and list comprehension to create a list of key-value pairs where the values come first, and then the keys. It finally prints the ordered key-value pairs.
Python3
test_dict = { "Gfg" : 1 , "is" : 3 , "Best" : 2 }
print ( "The original dictionary is : " + str (test_dict))
res = [val for val in zip (test_dict.values(), test_dict.keys())]
print ( "The ordered keys and values : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : [(1, 'Gfg'), (3, 'is'), (2, 'Best')]
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n), as we are creating a list of n items using the list comprehension.
Method #5: Using sorted() function and list comprehension
Step-by-step approach:
- Initialize the dictionary test_dict.
- Print the original dictionary using the print() function.
- Use the sorted() function to get the keys in alphabetical order, and assign the result to the keys variable.
- Use a list comprehension to get the values corresponding to each key in the keys list, and assign the result to the values variable.
- Concatenate the keys and values lists using the + operator, and assign the result to the res variable.
- Print the ordered keys and values using the print() function.
Python3
test_dict = { "Gfg" : 1 , "is" : 3 , "Best" : 2 }
print ( "The original dictionary is : " + str (test_dict))
keys = sorted (test_dict.keys())
values = [test_dict[key] for key in keys]
res = keys + values
print ( "The ordered keys and values : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : ['Best', 'Gfg', 'is', 2, 1, 3]
Time complexity: Sorting takes O(n log n) time, and list comprehension takes O(n) time. Therefore, the overall time complexity of this method is O(n log n).
Auxiliary space: This method creates two additional lists (keys and values) of size n, so the auxiliary space complexity is O(n).
Please Login to comment...