Introduction

In Python, values() is an inbuilt function that returns a view object. It has the values of the dictionary, as a list. Now if we check the type of the return value we will get the “dict_values” object. It needs to be cast to obtain the actual list.

Syntax

dictionary_name.values()

Parameters

No parameters are needed.

Returns

Returns a list of all the values available in a given dictionary.
The values have been stored in a reversed manner.

Error

No error is expected because no parameters are required.

Examples

Code 1

dictionary = {"A": 3, "B": 2, "C": 3}
print(dictionary.values()) 
 
 
# string values
dictionary = {"A": "4", "B": "1", "C": "3"}
print(dictionary.values())  

Output

dict_values([3, 2, 3])
dict_values(['4', '1', '3'])

Code 2

s = {"A" : 1, "B" : 2, "C" : 3}
l = s.values()
print(sum(l))

Output

6

write your code here: Coding Playground

Python Dictionary get()

If we want to access the value of a key we use the get() method.

Syntax

The Syntax of the get() method is:

dict.get(key[, value])

Parameters

In the get() method, there are two parameters that the method takes.

key- the key will be searched whose value is to be searched.
value (optional)- If the key is not returned then the value passed will be returned. The default value returned will be None.

Returns

get() method returns:

  • If the key is in the dictionary, it will return its value.
  • If the key is not found and the value is not specified it will return None.
  • If the key is not found and the value is specified then it returns that value.

Examples

Code 1

marks = {'A1':67, 'A2':87}
print(marks.get('A1'))

Output

67

Code 2

p = {'Name': 'Akash', 'Age': 18}
print('Name: ', p.get('Name'))
print('Age: ', p.get('Age'))
print('Salary: ', p.get('salary'))
print('Salary: ', p.get('salary', 50000.0))

Output

Name:  Akash
Age:  18
Salary:  None
Salary:  50000.0

write your code here: Coding Playground

Python get() method Vs dict[key]

The get() method will return a default value that is specified if the key is not found. In the case of dict[key], a key error exception is raised.

Code

p = {}
print('Salary: ', p.get('Salary'))
print(p['Salary'])

Output

File "main.py", line 3, in <module>
    print(p['Salary'])
KeyError: 'Salary'

write your code here: Coding Playground

Some more examples

Code 1

Cars = {
  "Brand": "Audi",
  "Model": "A7",
  "Year": 2018
}

x = Cars.values()
print(x)

Output

dict_values(['Audi', 'A7', 2018])

Code 2

Student = {
  "Name": "Ayush",
  "Section": "614-A",
  "Year": 2018,
  "UID": 4392
}

x = Student.values()
print(x)

Output

dict_values(['Ayush', '614-A', 2018, 4392])

Code 3

Staff = {
  "Name": "Ankush",
  "Section": "614-A",
  "Year": 2018,
  "UID": 4392,
  "Subject": "Java"
}

x = Staff.get('Name')
y = Staff.values()
print(x)
print(y)

Output

Ankush
dict_values(['Ankush', '614-A', 2018, 4392, 'Java'])

write your code here: Coding Playground

Conclusion

  • In Python, values() is an inbuilt function that returns a view object. It has the values of the dictionary, as a list.
  • The values() returns a list of all the values available in a given dictionary. The values have been stored in a reversed manner.
  • If we want to access the value of a key we use the get() method.
  • The get() method will return a default value that is specified if the key is not found. In the case of dict[key], a key error exception is raised.