w3resource

Python: Create a flat list of all the values in a flat dictionary

Python dictionary: Exercise-79 with Solution

Write a Python program to create a flat list of all the values in a flat dictionary.

  • Use dict.values() to return the values in the given dictionary.
  • Return a list() of the previous result.

Sample Solution:

Python Code:

# Define a function 'test' that takes a dictionary 'flat_dict'.
# The function returns a list of values from the given dictionary.
def test(flat_dict):
    # Use the 'values' method to extract all the values from the dictionary and convert them to a list.
    return list(flat_dict.values())

# Create a dictionary 'students' with key-value pairs.
students = {
    'Theodore': 19,
    'Roxanne': 20,
    'Mathew': 21,
    'Betty': 20
}

# Print the original dictionary.
print("\nOriginal dictionary elements:")
print(students)

# Call the 'test' function to create a flat list of all the values in the 'students' dictionary.
print("\nCreate a flat list of all the values of the said flat dictionary:")
print(test(students))

Sample Output:

Original dictionary elements:
{'Theodore': 19, 'Roxanne': 20, 'Mathew': 21, 'Betty': 20}

Create a flat list of all the values of the said flat dictionary:
[19, 20, 21, 20]

Flowchart:

Flowchart: Create a flat list of all the values in a flat dictionary.

Python Code Editor:

Previous: Write a Python program to create a flat list of all the keys in a flat dictionary.
Next: Write a Python program to find the key of the maximum value in a dictionary.

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-79.php