Dictionary values() Function in Python:
The values() method returns a view object that displays a list of all the dictionary’s values.
Syntax:
dictionary.values()
Parameters: This method doesn’t accept any parameters
Return Value:
The values() method returns a view object containing a list of all values in a given dictionary.
Examples:
Example1:
Input:
Given dictionary = {20: 'good', 30: 'morning', 40: 'btechgeeks'}Output:
The values of the given dictionary is: dict_values(['good', 'morning', 'btechgeeks'])
Example2:
Input:
Given dictionary = {'hello': 1, 'this': 3, 'is': 4, 'btechgeeks': 8}Output:
The values of the given dictionary is: dict_values([1, 3, 4, 8])
Dictionary values() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the dictionary as static input and store it in a variable.
- Get all the values of the given dictionary using the values() method.
- Store it in another variable.
- Print all the values of the given dictionary.
- The Exit of the Program.
Below is the implementation:
# Give the dictionary as static input and store it in a variable.
gvn_dict = {20: 'good', 30: 'morning', 40: 'btechgeeks'}
# Get all the values of the given dictionary using the values() method.
# Store it in another variable.
dict_valus = gvn_dict.values()
# Print all the values of the given dictionary.
print("The values of the given dictionary is:")
print(dict_valus)
Output:
The values of the given dictionary is: dict_values(['good', 'morning', 'btechgeeks'])
Method #2: Using Built-in Functions (User Input)
Approach:
- Take a dictionary and initialize it with an empty dictionary using dict() or {}.
- Give the number of keys as user input using int(input()) and store it in a variable.
- Loop till the given number of keys using for loop.
- Inside the for loop scan the key and value as user input using input(), split() functions, and store them in two separate variables.
- Initialize the key with the value of the dictionary.
- Get all the values of the given dictionary using the values() method.
- Store it in another variable.
- Print all the values of the given dictionary.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.
gvn_dict = {}
# Give the number of keys as user input using int(input()) and store it in a variable.
numb_of_kys = int(
input('Enter some random number of keys of the dictionary = '))
# Loop till the given number of keys using for loop.
for p in range(numb_of_kys):
# Inside the for loop scan the key and value as
# user input using input(),split() functions
# and store them in two separate variables.
keyy, valuee = input(
'Enter key and value separated by spaces = ').split()
# Initialize the key with the value of the dictionary.
gvn_dict[keyy] = valuee
# Get all the values of the given dictionary using the values() method.
# Store it in another variable.
dict_valus = gvn_dict.values()
# Print all the values of the given dictionary.
print("The values of the given dictionary is:")
print(dict_valus)
Output:
Enter some random number of keys of the dictionary = 4 Enter key and value separated by spaces = hello 350 Enter key and value separated by spaces = this 450 Enter key and value separated by spaces = is 550 Enter key and value separated by spaces = btechgeeks 650 The values of the given dictionary is: dict_values(['350', '450', '550', '650'])