Open In App

Python | Segregating Key’s Values

Improve
Improve
Like Article
Like
Save
Share
Report

Many times when we have the requirement to separate the values of a dictionary key in case we have a list of dictionary and need to separate it’s different key’s values. This is quite useful utility used in web development. Let’s discuss certain ways in which this can be done. 
Method #1: Using list comprehension + tuple() The list comprehension can be coupled with tuple function and can be used to perform this particular task. List comprehension does the task of segregation and tuple function is used put them into separate tuples. 

Python3




# Python3 code to demonstrate
# segregation of keys and values
# using list comprehension + tuple()
 
# initializing list of dictionaries
test_list = [{'Nikhil' : 1, 'Akash' : 2},
             {'Nikhil' : 3, 'Akash' : 4}]
 
# printing original list
print("The original list : " +  str(test_list))
 
# using list comprehension + tuple()
# to segregate keys and values
res = [tuple(i["Nikhil"] for i in test_list), tuple(i["Akash"]
                                          for i in test_list)]
 
# printing result
print("The segregated keys and values : " + str(res))


Output : 

The original list : [{'Akash': 2, 'Nikhil': 1}, {'Akash': 4, 'Nikhil': 3}]
The segregated keys and values : [(1, 3), (2, 4)]

Time complexity: O(n*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 #2 : Using map() + zip() + list() These functions can also be coupled to achieve this particular functionality. The map function is used to extract the values, zip function is used to do the segregation. And list function is used at end to bind the result into a dictionary. 

Python3




# Python3 code to demonstrate
# segregation of keys and values
# using map() + list() + zip()
 
# initializing list of dictionaries
test_list = [{'Nikhil' : 1, 'Akash' : 2},
             {'Nikhil' : 3, 'Akash' : 4}]
 
# printing original list
print("The original list : " +  str(test_list))
 
# using map() + list() + zip()
# to segregate keys and values
res = list(zip(*map(dict.values, test_list)))
 
# printing result
print("The segregated keys and values : " + str(res))


Output : 

The original list : [{'Akash': 2, 'Nikhil': 1}, {'Akash': 4, 'Nikhil': 3}]
The segregated keys and values : [(2, 4), (1, 3)]

Method #3: Using nested loop

Approach

We are iterating over the dictionaries and their key-value pairs using nested loops. We are using a dictionary to collect the values associated with each key and finally, creating a list of tuples containing the key and its associated values.

Algorithm

1. Create the original list of dictionaries
2. Create an empty dictionary called segregated
3. Iterate over each dictionary in the original list
4. Iterate over each key-value pair in the dictionary
5. Append the value associated with each key to the segregated dictionary
6. Create a list of tuples containing the key and its associated values, sorted by key
7. Print the resulting list

Python3




# original list of dictionaries
original_list = [{'Akash': 2, 'Nikhil': 1}, {'Akash': 4, 'Nikhil': 3}]
 
# segregating keys and values
segregated = {}
for dictionary in original_list:
    for key, value in dictionary.items():
        segregated.setdefault(key, []).append(value)
 
result = [(key, tuple(segregated[key])) for key in sorted(segregated)]
 
# printing the result
print("The segregated keys and values:", result)


Output

The segregated keys and values: [('Akash', (2, 4)), ('Nikhil', (1, 3))]

Time complexity:  O(n * m), where n is the number of dictionaries in the original list and m is the average number of key-value pairs in each dictionary.

Auxiliary Space: O(n * m), as it creates a dictionary ‘segregated’ that holds all the keys and values from the original list. Then, it creates a list ‘result’ that holds tuples of keys and their corresponding values.



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads