w3resource

Python: Extract a list of values from a given list of dictionaries

Python dictionary: Exercise-52 with Solution

Write a Python program to extract a list of values from a given list of dictionaries.

Visual Presentation:

Python Dictionary: Extract a list of values from a given list of dictionaries.

Sample Solution:

Python Code:

# Define a function 'test' that extracts a list of values for a specific subject ('marks') from a list of dictionaries ('lst').
def test(lst, marks):
    # Use a list comprehension to extract the values associated with the subject ('marks') in each dictionary.
    result = [d[marks] for d in lst if marks in d]
    return result

# Create a list of dictionaries 'marks' with subjects 'Math' and 'Science' and associated marks.
marks = [{'Math': 90, 'Science': 92}, 
         {'Math': 89, 'Science': 94}, 
         {'Math': 92, 'Science': 88}]

# Print a message indicating the start of the code section and the original list of dictionaries.
print("\nOriginal Dictionary:")
print(marks)

# Define the subject 'subj' as "Science".
subj = "Science"

# Print a message indicating the intention to extract values for the subject 'subj' from the list of dictionaries.
print("\nExtract a list of values from the said list of dictionaries where subject =", subj)

# Call the 'test' function to extract the values associated with the subject 'subj' and print the result.
print(test(marks, subj))

# Print a message indicating the start of the code section and the original list of dictionaries.
print("\nOriginal Dictionary:")
print(marks)

# Define the subject 'subj' as "Math".
subj = "Math"

# Print a message indicating the intention to extract values for the subject 'subj' from the list of dictionaries.
print("\nExtract a list of values from the said list of dictionaries where subject =", subj)

# Call the 'test' function to extract the values associated with the subject 'subj' and print the result.
print(test(marks, subj)) 

Sample Output:

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]

Flowchart:

Flowchart: Extract a list of values from a given list of dictionaries.

Python Code Editor:

Previous: A Python Dictionary contains List as value. Write a Python program to update the list values in the said dictionary.
Next: Write a Python program to find the length of a given dictionary values.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/dictionary/python-data-type-dictionary-exercise-52.php