In Python, dictionaries are useful data structures that use keys for indexing. They are an unordered sequence of items (key-value pairs), so the order does not matter. The keys are not able to be changed. Dictionaries, like lists, can hold a variety of data types, including integers, floats, strings, NaN, Booleans, lists, arrays, and even nested dictionaries.
Given a dictionary, the task is to iterate over all values of the dictionary .
Examples:
Input:
dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}
Output:
600 300 900
Traverse the values of Dictionary
There are several ways to traverse the values of dictionary some of them are:
Method #1:Using for loop
To iterate over the keys of a dictionary, a dictionary object can also be used as an iterable object. We can easily iterate over all keys in the dictionary if we use it with a for loop. The value associated with that key can then be selected during iteration.
Below is the implementation:
# Given dictionary dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900} # using for loop to traverse the dictionary for key in dictionary: print(dictionary[key])
Output:
600 300 900
Method #2:Using items()
The dictionary class in Python has a function items() that returns an iterable sequence of all the dictionary’s key-value pairs, dict items. It shows all of the dictionary’s items (key-value pairs), so any changes to the original dictionary will be reflected in this sequence. We can’t use indexing with this sequence, either. This can be used in conjunction with a for loop to iterate over all pairs in the dictionary, and we can select the second element of the pair / tuple, i.e. the value, while iterating.
Below is the implementation:
# Given dictionary dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900} # using items() for key, value in dictionary.items(): # printing the value print(value)
Output:
600 300 900
Method #3:Using values()
The dictionary class in Python has a function values() that returns an iterable sequence of all dictionary values, i.e. dict values. It is a view of the dictionary’s entire value set, which means that any changes to the original dictionary will be reflected in this sequence. We can’t use indexing with this sequence, either. However, we can use this in conjunction with a for loop to iterate through all of the dictionary’s values.
Below is the implementation:
# Given dictionary dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900} # using values() for value in dictionary.values(): # printing the value print(value)
Output:
600 300 900
Method #4:Using list Comprehension
This list comprehension can also be used to iterate through all of the dictionary’s values and print each one separately.
Below is the implementation:
# Given dictionary dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900} # using list comprehension print([value for value in dictionary.values()])
Output:
[600, 300, 900]
Method #5: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 = {'Hello': 600, 'world': 300, 'BTechGeeks': 900} # converting given dictionary values to list using values() function list_values = list(dictionary.values()) # print the dictionary values for value in list_values: print(value)
Output:
600 300 900
Related Programs:
- python iterate loop over all nested dictionary values
- python how to create a list of all the values in a dictionary
- python iterate over dictionary all key value pairs
- pandas loop or iterate over all or certain columns of a dataframe
- python iterate over dictionary with list values
- python print all values of a dictionary
- python how to create a list of all the keys in the dictionary