While working with dictionaries, sometimes we might have a problem in which we need to reduce the space taken by single container and wish to divide the dictionary into 2 halves. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using items() + len() + list slicing The combination of above functions can easily perform this particular task in which slicing into half is done by list slicing and items of dictionary are extracted by items()
Python3
# Python3 code to demonstrate working of # Split dictionary by half # Using items() + len() + list slicing # Initialize dictionary test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Using items() + len() + list slicing # Split dictionary by half res1 = dict ( list (test_dict.items())[ len (test_dict) / / 2 :]) res2 = dict ( list (test_dict.items())[: len (test_dict) / / 2 ]) # printing result print ( "The first half of dictionary : " + str (res1)) print ( "The second half of dictionary : " + str (res2)) |
The original dictionary : {'gfg': 6, 'is': 4, 'for': 2, 'CS': 10} The first half of dictionary : {'for': 2, 'CS': 10} The second half of dictionary : {'gfg': 6, 'is': 4}
The time complexity of the given program is O(N), where N is the number of key-value pairs in the dictionary.
The auxiliary space complexity of the program is O(N), where N is the number of key-value pairs in the dictionary.
Method #2 : Using slice() + len() + items() The combination of above functions can be used to perform this particular task. In this, we perform task similar to above method, just the difference is the slice operation is performed by slice() instead of list slicing.
Python3
# Python3 code to demonstrate working of # Split dictionary by half # Using items() + len() + slice() from itertools import islice # Initialize dictionary test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Using items() + len() + slice() # Split dictionary by half inc = iter (test_dict.items()) res1 = dict (islice(inc, len (test_dict) / / 2 )) res2 = dict (inc) # printing result print ( "The first half of dictionary : " + str (res1)) print ( "The second half of dictionary : " + str (res2)) |
The original dictionary : {'gfg': 6, 'is': 4, 'for': 2, 'CS': 10} The first half of dictionary : {'gfg': 6, 'is': 4} The second half of dictionary : {'for': 2, 'CS': 10}
Method #3 : Using dict() + zip()
This method uses the zip function to create two lists of keys and values from the original dictionary and then uses the dict function to convert those lists back into two separate dictionaries.
Python3
# Python3 code to demonstrate working of # Split dictionary by half # Using dict() + zip() # Initialize dictionary test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Using dict() + zip() # Split dictionary by half keys, values = zip ( * test_dict.items()) half = len (test_dict) / / 2 res1 = dict ( zip (keys[:half], values[:half])) res2 = dict ( zip (keys[half:], values[half:])) # printing result print ( "The first half of dictionary : " + str (res1)) print ( "The second half of dictionary : " + str (res2)) #This code is contributed by Edula Vinay Kumar Reddy |
The original dictionary : {'gfg': 6, 'is': 4, 'for': 2, 'CS': 10} The first half of dictionary : {'gfg': 6, 'is': 4} The second half of dictionary : {'for': 2, 'CS': 10}
Time complexity is O(n) as we are iterating through all the elements of the dictionary once. Auxiliary Space is also O(n) as we are creating new dictionaries of the same size as the original dictionary.
Method #3 : dict() constructor:
Python3
# Initialize dictionary test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 } # printing original dictionary print ( "The original dictionary : " , test_dict) # Convert dictionary to list of tuples items = list (test_dict.items()) # Split the list of tuples in half first_half = items[: len (items) / / 2 ] second_half = items[ len (items) / / 2 :] # Create two separate dictionaries first_half_dict = dict (first_half) second_half_dict = dict (second_half) # Printing result print ( "The first half of dictionary : " , first_half_dict) print ( "The second half of dictionary : " , second_half_dict) #This code is contributed by Jyothi pinjala. |
The original dictionary : {'gfg': 6, 'is': 4, 'for': 2, 'CS': 10} The first half of dictionary : {'gfg': 6, 'is': 4} The second half of dictionary : {'for': 2, 'CS': 10}
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using List comprehension
Steps:
- Initialize the dictionary.
- Calculate the length of the dictionary and divide it by 2 to get half of it.
- Using a for loop, iterate through the items of the dictionary.
- Check if the index of the item is less than half, then add it to the first half dictionary. Else, add it to the second half dictionary.
- Print the first and second half of the dictionary.
Python3
# Python program for the above approach # Initialize dictionary test_dict = { 'gfg' : 6 , 'is' : 4 , 'for' : 2 , 'CS' : 10 } # Print the original dictionary print ( "The original dictionary : " + str (test_dict)) # Split the dictionary by half using # the list comprehension half = len (test_dict) / / 2 res1 = {k: v for i, (k, v) in enumerate (test_dict.items()) if i < half} res2 = {k: v for i, (k, v) in enumerate (test_dict.items()) if i > = half} # Print the result print ( "The first half of dictionary : " + str (res1)) print ( "The second half of dictionary : " + str (res2)) |
The original dictionary : {'gfg': 6, 'is': 4, 'for': 2, 'CS': 10} The first half of dictionary : {'gfg': 6, 'is': 4} The second half of dictionary : {'for': 2, 'CS': 10}
Time Complexity: O(n)
The time complexity of this algorithm is O(n) as we are iterating over all the items of the dictionary only once.
Space Complexity: O(n)
The space complexity of this algorithm is O(n) as we are creating two new dictionaries to store the first and second half of the original dictionary, which can have a maximum size of n.