Given a Dictionary, the task is to write a python program to swap positions of dictionary items. The code given below takes two indices and swap values at those indices.
Input : test_dict = {‘Gfg’ : 4, ‘is’ : 1, ‘best’ : 8, ‘for’ : 10, ‘geeks’ : 9}, i, j = 1, 3
Output : {‘Gfg’: 4, ‘for’: 10, ‘best’: 8, ‘is’: 1, ‘geeks’: 9}
Explanation : (for : 10) and (is : 1) swapped order.
Input : test_dict = {‘Gfg’ : 4, ‘is’ : 1, ‘best’ : 8, ‘for’ : 10, ‘geeks’ : 9}, i, j = 2, 3
Output : {‘Gfg’: 4, ‘is’: 1, ‘for’: 10, ‘best’: 8, ‘geeks’: 9}
Explanation : (for : 10) and (best : 8) swapped order.
Method : Using items() and dict()
This task is achieved in 3 steps:
- First dictionary is converted to equivalent key value pairs in form of tuples,
- Next swap operation is performed in a Pythonic way.
- At last, tuple list is converted back to dictionary, in its required format.
Example:
Python3
test_dict = {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'geeks': 9}
print("The original dictionary is : " + str(test_dict))
i, j = 1, 3
tups = list(test_dict.items())
tups[i], tups[j] = tups[j], tups[i]
res = dict(tups)
print("The swapped dictionary : " + str(res))
|
Output:
The original dictionary is : {‘Gfg’: 4, ‘is’: 1, ‘best’: 8, ‘for’: 10, ‘geeks’: 9}
The swapped dictionary : {‘Gfg’: 4, ‘for’: 10, ‘best’: 8, ‘is’: 1, ‘geeks’: 9}
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2 ; dictionary manipulation using built-in functions and data types.
Here are the steps:
Initialize a dictionary test_dict with some key-value pairs.
Print the original dictionary.
Initialize two variables i and j with the indices of the key-value pairs that need to be swapped.
Convert the dictionary to a list of tuples using the items() method.
Swap the tuples at indices i and j in the list using tuple unpacking.
Convert the list of tuples back to a dictionary using the dict() method.
Print the swapped dictionary.
Python3
test_dict = {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'geeks': 9}
print("The original dictionary is : " + str(test_dict))
i, j = 1, 3
tups = list(test_dict.items())
tups[i], tups[j] = tups[j], tups[i]
res = dict(tups)
print("The swapped dictionary : " + str(res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'geeks': 9}
The swapped dictionary : {'Gfg': 4, 'for': 10, 'best': 8, 'is': 1, 'geeks': 9}
The time complexity of this program is O(n), where n is the number of key-value pairs in the dictionary.
The space complexity of the program is O(n), as we are creating a new list of tuples to store the key-value pairs, and then creating a new dictionary from this list.
METHOD 3:Using loop
APPROACH:
The problem is to swap the positions of key-value pairs in a dictionary in Python using a loop.
ALGORITHM:
1.Create an empty dictionary swapped_dict to store the swapped key-value pairs.
2.Iterate over the key-value pairs of the original dictionary original_dict using a for loop.
3.For each key-value pair, swap the positions of the key and value and add them to the swapped_dict dictionary.
4.Print the swapped_dict dictionary.
Python3
original_dict = {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'geeks': 9}
swapped_dict = {}
for key, value in original_dict.items():
swapped_dict[value] = key
print("The swapped dictionary :", swapped_dict)
|
Output
The swapped dictionary : {4: 'Gfg', 1: 'is', 8: 'best', 10: 'for', 9: 'geeks'}
Time Complexity:
The time complexity of the algorithm is O(n), where n is the number of key-value pairs in the dictionary. This is because we need to iterate over each key-value pair once.
Space Complexity:
The space complexity of the algorithm is also O(n), where n is the number of key-value pairs in the dictionary. This is because we need to create an empty dictionary to store the swapped key-value pairs. The size of this dictionary will be the same as the original dictionary.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
04 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...