Python Dictionary: Values() Function

Python’s implementation of an associative array data structure is dictionaries. A dictionary is a collection of key/value pairs. A key pair and its associated value are used to represent each key pair.

A dictionary is a list of key-value pairs enclosed in curly braces and separated by commas. The column ‘:’ separates the value of each key.

Sorting a dictionary solely for the purpose of obtaining a representation of the sorted dictionary is not possible. By default, dictionary entries are ordered, but other data types, such as lists and tuples, are not. As a result, you’ll need an ordered data form, such as a list—probably a list of tuples.

Dictionary values() function & examples of it

values() is a built-in Python method that returns a list of all the values available in a given dictionary.

Syntax:

dictionary_name.values()

Parameters:

There is no need to pass parameters.

Return type:

It returns a sequence containing a view of all dictionary values. Because the sequence is just a view of values, any change to a value in the dictionary will be reflected in the sequence as well.

1)Display all the values of the dictionary

We can display all the values of the dictionary by using the given syntax above.

Implementation:

# given dictionary
dictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}
# getting all the values from the given dictionary
dictvalues = dictionary.values()
# printing the values
print(dictvalues)

Output:

dict_values([700, 200, 100, 300])

2)Update/Modify the values of the dictionary

If we first fetch all of the dictionary’s values with the values() function and then modify the dictionary, the changes will be reflected in the sequence of the previously fetched values as well.

Below is the implementation:

# given dictionary
dictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}
# getting all the values from the given dictionary
dictvalues = dictionary.values()
# print the values before modification
print("Before Modification", dictvalues)
# updating value of BTechGeeks to 1000
dictionary['BTechGeeks'] = 1000
# printing the after modification
print("After Modification", dictvalues)

Output:

Before Modification dict_values([700, 200, 100, 300])
After Modification dict_values([700, 200, 100, 1000])

3)Conversion of dictionary 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

Below is the implementation:

# given dictionary
dictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}
# getting all the values from the given dictionary and converting to list
dictvalues = list(dictionary.values())
# printing the dictionary values list
print(dictvalues)

Output:

[700, 200, 100, 300]

4)Display Maximum and minimum values

We can display maximum and minimum values by using max() and min() functions .

Below is the implementation:

# given dictionary
dictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}
# getting all the values from the given dictionary and converting to list
dictvalues = dictionary.values()
# Getting max and min values
maxvalue = max(dictvalues)
minvalue = min(dictvalues)
# print the max value
print("Maximum value in dictionary", maxvalue)
print("Minimum value in dictionary", minvalue)

Output:

Maximum value in dictionary 700
Minimum value in dictionary 100

5)Display sum of dictionary values

We can display sum of dictionary values by using sum() function .

Below is the implementation:

# given dictionary
dictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}
# getting all the values from the given dictionary and converting to list
dictvalues = dictionary.values()
# Getting sum of dictionary values
valuesum = sum(dictvalues)
# print sum of values
print("Sum of dictionary values", valuesum)

Output:

Sum of dictionary values 1300

6)Display Average of dictionary values

We can display average of dictionary values with the help of sum() and len() functions.

Below is the implementation:

# given dictionary
dictionary = {'Hello': 700, 'This': 200, 'is': 100, 'BTechGeeks': 300}
# getting all the values from the given dictionary and converting to list
dictvalues = dictionary.values()
# Getting average of dictionary values
averagevalue = sum(dictvalues)/len(dictvalues)
# print average of values
print("Average of dictionary values", averagevalue)

Output:

Average of dictionary values 325.0

Related Programs: