Python: Print all Values of a Dictionary

Dictionaries are Python’s implementation of an associative list, which is a data structure. A dictionary is a collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.

Given a dictionary, the task is to print all the values of the dictionary.

Examples:

Input:

dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}

Output:

200
100
300

Display all values of the Dictionary

There are several ways to display all the values of the dictionary some of them are:

Method #1:Using for loop and values() function

The dictionary class in Python has a function dict.values() that returns an iterable sequence of dictionary values. We can iterate over the sequence of values returned by the function values() using a for loop, and we can print each value while iterating.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# Traverse the dictionary using for loop
for value in dictionary.values():
    # print the values
    print(value)

Output:

200
100
300

Method #2:Converting values to list

The sequence returned by the values() function can be passed to the list to create a list of all values in the dictionary .

We use list() function to achieve this. Print the list(values)

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# converting values to list
valueslist = list(dictionary.values())
# print the values list
for value in valueslist:
    print(value)

Output:

200
100
300

Method #3:UsingĀ  list Comprehension

We can also use this list comprehension to iterate through all of the dictionary’s values and print each one individually.

Below is the implementation:

# Given dictionary
dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}
# using list comprehension
print([value for value in dictionary.values()])

Output:

[200, 100, 300]

Related Programs: