get values in a python dictionary

Get Values of a Python Dictionary – With Examples

In this tutorial, we will look at how to get the values of a Python dictionary with the help of some examples.

get values in a python dictionary

You can use the Python dictionary values() function to get all the values in a Python dictionary. The following is the syntax:

# get all the values in a dictionary
sample_dict.values()

It returns a dict_values object containing all the values in the dictionary. This object is iterable, that is, you can use it to iterate through the values in the dictionary. You can use the list() function to convert this object into a list.

Let’s look at some examples.

Let’s create a dictionary containing the names to department mappings of employees at an office. The keys here are the employee names whereas the values are their respective departments.

Let’s get all the values in the dictionary using the dictionary’s values() function.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# get values of dictionary
print(employees.values())

Output:

dict_values(['Sales', 'Sales', 'Accounting'])

You can see that we get all the names of the departments (the values in the dictionary employee) in a dict_values object.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Now, you can also convert this object to a list using the Python built-in list() function.

# dictionary values as list
print(list(employees.values()))

Output:

['Sales', 'Sales', 'Accounting']

We now have the values in the dictionary employees as a list.

Alternatively, you can iterate through the dictionary items and append the value in each iteration to a result list.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# get values of dictionary
val_ls = []
for key, val in employees.items():
    val_ls.append(val)
print(val_ls)

Output:

['Sales', 'Sales', 'Accounting']

We get the values in the dictionary as a list.

You can also reduce the above computation to a single line using list comprehension.

# get values of dictionary
val_ls = [val for key, val in employees.items()]
print(val_ls)

Output:

['Sales', 'Sales', 'Accounting']

We get the same result as above.

In this tutorial, we looked at different ways to get all the values in a Python dictionary. Using the dictionary’s values() function is a simpler and a direct way of getting the values as compared to the iteration-based methods.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top