Python – Values from custom List in Records

Sometimes, while working with Python records, we can have a problem in which we need to extract all the values from the custom list in list dictionary records. This problem can have application in domains such as web development and school programming. Let’s discuss certain ways in which this task can be performed.

Input :
test_list = [{‘name’ : ‘Gfg’, ‘id’ : 1, ‘Score’ : 3}, {‘name’ : ‘is’, ‘id’ : 4, ‘Score’ : 10},
{‘name’ : ‘Best’, ‘Score’ : 12}]
get_list = [‘name’]
Output : [[‘Gfg’], [‘is’], [‘Best’]]

Input :
test_list = [{‘name’ : ‘Gfg’, ‘id’ : 1, ‘Score’ : 3}, {‘name’ : ‘is’, ‘id’ : 4, ‘Score’ : 10},
{‘name’ : ‘Best’, ‘Score’ : 12}]
get_list = [‘Serial’]
Output : [[None], [None], [None]]

Method #1 : Using list comprehension + get()
The combination of above functions can be used to solve this problem. In this, we perform the task of iterating through each dictionary using list comprehension and get() is used to fetch dictionary values and also handle for non present values.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Values from custom List in Records
# Using list comprehension + get()
  
# initializing list
test_list = [{'name' : 'Gfg', 'id' : 1, 'Score' : 3},
             {'name' : 'is', 'id' : 4, 'Score' : 10},
             {'name' : 'Best', 'Score' : 12}]
  
# printing original list 
print("The original list : " + str(test_list))
  
# initializing Get list 
get_list = ['name', 'id']
  
# Values from custom List in Records
# Using list comprehension + get()
res = [list(idx.get(sub) for sub in get_list) for idx in test_list]
  
# printing result 
print("All extracted values : " + str(res))

chevron_right


Output :



The original list : [{'name': 'Gfg', 'id': 1, 'Score': 3}, {'name': 'is', 'id': 4, 'Score': 10}, {'name': 'Best', 'Score': 12}]
All extracted values : [['Gfg', 1], ['is', 4], ['Best', None]]

 

Method #2 : Using list comprehension + itemgetter() + intersection()
The combination of above functions can be used to solve this problem. In this, we use itemgetter() and intersection() to get the value checking both target list and argument list. Can’t handle not present keys.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Values from custom List in Records
# Using list comprehension + itemgetter() + intersection()
from operator import itemgetter
  
# initializing list
test_list = [{'name' : 'Gfg', 'id' : 1, 'Score' : 3},
             {'name' : 'is', 'id' : 4, 'Score' : 10}]
  
# printing original list 
print("The original list : " + str(test_list))
  
# initializing Get list 
get_list = ['name', 'id']
  
# Values from custom List in Records
# Using list comprehension + itemgetter() + intersection()
res = [list(itemgetter(*set(get_list).intersection(idx))(idx)) for idx in test_list]
  
# printing result 
print("All extracted values : " + str(res))

chevron_right


Output :

The original list : [{'name': 'Gfg', 'id': 1, 'Score': 3}, {'name': 'is', 'id': 4, 'Score': 10}]
All extracted values : [[1, 'Gfg'], [4, 'is']]



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.