Python Dictionary values() Method (with Examples)

Python Dictionary values() Method: In this tutorial, we will learn about the values() method of a dictionary with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 12, 2023

Python Dictionary values() Method

The values() is an inbuilt method of dict class that is used to get all values of a dictionary, it returns a view object that contains the all values of the dictionary as a list. The method is called with this dictionary and returns dictionary values.

Syntax

The following is the syntax of values() method:

dictionary_name.values()

Parameter(s):

The following are the parameter(s):

  • None - It does not accept any parameter.

Return Value

The return type of this method is <class 'dict_values'>, it returns all values as a view object that contains a list of all values.

Example 1: Use of Dictionary values() Method

alphabets = {
    "a": "Apple", 
    "b": "Bat", 
    "c": "Cat"
}

result = alphabets.values()

print("Values:", result)

Output

Values: dict_values(['Apple', 'Bat', 'Cat'])

Example 2: Use of Dictionary values() Method

# dictionary declaration
student = {
  "roll_no": 101,
  "name": "Shivang",
  "course": "B.Tech",
  "perc" : 98.5
}

# printing dictionary
print("data of student dictionary...")
print(student)

# getting all values
x = student.values()
print(x)

# printing type of values() Method
print('Type is: ',type(student.values()))

# changing the value
# it will effect the value of view object also
student['course'] = 'MCA'

# printing dictionary
print("data of student dictionary...")
print(student)

# getting all values
x = student.values()
print(x)

Output

data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'B.Tech', 'perc': 98.5}
dict_values([101, 'Shivang', 'B.Tech', 98.5])
Type is:  <class 'dict_values'>
data of student dictionary...
{'roll_no': 101, 'name': 'Shivang', 'course': 'MCA', 'perc': 98.5}
dict_values([101, 'Shivang', 'MCA', 98.5])

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.