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.
# 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)) |
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.
# 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)) |
The original list : [{'Akash': 2, 'Nikhil': 1}, {'Akash': 4, 'Nikhil': 3}]
The segregated keys and values : [(2, 4), (1, 3)]
Recommended Posts:
- Python | Segregating key's value in list of dictionaries
- Python | Visualize missing values (NaN) values using Missingno Library
- Python | Merge Python key values to list
- Python dictionary | values()
- Python | Remove None values from list
- Python | Combine the values of two dictionaries having same key
- Python | Unique values in Matrix
- Python | Get unique values from a list
- Python | Check for None values in given dictionary
- Python | Check if all values are 0 in dictionary
- Python | Check if all the values in a list are less than a given value
- Python | Filter even values from a list
- Returning Multiple Values in Python
- Python | Sum values for each key in nested dictionary
- Python | Pandas DataFrame.values
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.



