Python | Segregating Key’s Values

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# segragation 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))

chevron_right


Output :



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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# segragation 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))

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.