Friday, December 27, 2024
Google search engine
HomeLanguagesPython Program to Swap dictionary item’s position

Python Program to Swap dictionary item’s position

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, ‘Lazyroar’ : 9}, i, j = 1, 3

Output : {‘Gfg’: 4, ‘for’: 10, ‘best’: 8, ‘is’: 1, ‘Lazyroar’: 9}

Explanation : (for : 10) and (is : 1) swapped order.

Input : test_dict = {‘Gfg’ : 4, ‘is’ : 1, ‘best’ : 8, ‘for’ : 10, ‘Lazyroar’ : 9}, i, j = 2, 3

Output : {‘Gfg’: 4, ‘is’: 1, ‘for’: 10, ‘best’: 8, ‘Lazyroar’: 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




# initializing dictionary
test_dict = {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'Lazyroar': 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing swap indices
i, j = 1, 3
 
# conversion to tuples
tups = list(test_dict.items())
 
# swapping by indices
tups[i], tups[j] = tups[j], tups[i]
 
# converting back
res = dict(tups)
 
# printing result
print("The swapped dictionary : " + str(res))


Output:

The original dictionary is : {‘Gfg’: 4, ‘is’: 1, ‘best’: 8, ‘for’: 10, ‘Lazyroar’: 9}

The swapped dictionary : {‘Gfg’: 4, ‘for’: 10, ‘best’: 8, ‘is’: 1, ‘Lazyroar’: 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




# initializing dictionary
test_dict = {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'Lazyroar': 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing swap indices
i, j = 1, 3
 
# conversion to tuples
tups = list(test_dict.items())
 
# swapping by indices
tups[i], tups[j] = tups[j], tups[i]
 
# converting back to dictionary
res = dict(tups)
 
# printing result
print("The swapped dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'Lazyroar': 9}
The swapped dictionary : {'Gfg': 4, 'for': 10, 'best': 8, 'is': 1, 'Lazyroar': 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 dictionary
original_dict = {'Gfg': 4, 'is': 1, 'best': 8, 'for': 10, 'Lazyroar': 9}
 
# Create an empty dictionary to store the swapped items
swapped_dict = {}
 
# Swap the positions of items using a loop
for key, value in original_dict.items():
    swapped_dict[value] = key
 
# Print the swapped dictionary
print("The swapped dictionary :", swapped_dict)


Output

The swapped dictionary : {4: 'Gfg', 1: 'is', 8: 'best', 10: 'for', 9: 'Lazyroar'}

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.

RELATED ARTICLES

Most Popular

Recent Comments