Python
dictionary
values extraction
programming
coding tips

How can I get list of values from dict?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To get a list of values from a Python dictionary, use list(d.values()). The dict.values() method returns a view object, not a list. In Python 3, you must wrap it with list() if you need indexing, slicing, or other list operations. For most iteration purposes, the view object works directly without conversion.

Basic Usage

python
1d = {"name": "Alice", "age": 30, "city": "Seattle"}
2
3# Get values as a view object
4values_view = d.values()
5print(values_view)        # dict_values(['Alice', 30, 'Seattle'])
6print(type(values_view))  # <class 'dict_values'>
7
8# Convert to a list
9values_list = list(d.values())
10print(values_list)        # ['Alice', 30, 'Seattle']
11print(type(values_list))  # <class 'list'>

View Object vs List

python
1d = {"a": 1, "b": 2, "c": 3}
2
3# View object — reflects changes to the dictionary
4view = d.values()
5print(list(view))  # [1, 2, 3]
6
7d["d"] = 4
8print(list(view))  # [1, 2, 3, 4] — view updated automatically
9
10# List — snapshot at the time of creation
11values = list(d.values())
12d["e"] = 5
13print(values)  # [1, 2, 3, 4] — list not updated

The view is a live window into the dictionary. A list is an independent copy.

When You Need a List vs When You Don't

python
1d = {"x": 10, "y": 20, "z": 30}
2
3# Iteration — view is fine, no need for list()
4for value in d.values():
5    print(value)
6
7# Membership test — view is fine
8if 20 in d.values():
9    print("Found 20")
10
11# len() — view is fine
12print(len(d.values()))  # 3
13
14# Indexing — REQUIRES list
15# d.values()[0]  # TypeError: 'dict_values' object is not subscriptable
16values = list(d.values())
17print(values[0])  # 10
18
19# Slicing — REQUIRES list
20print(values[:2])  # [10, 20]
21
22# Sorting — REQUIRES list (or use sorted())
23print(sorted(d.values()))           # [10, 20, 30]
24print(sorted(d.values(), reverse=True))  # [30, 20, 10]

Getting Keys and Values Together

python
1d = {"apple": 3, "banana": 1, "cherry": 5}
2
3# Keys
4keys = list(d.keys())     # ['apple', 'banana', 'cherry']
5
6# Values
7values = list(d.values())  # [3, 1, 5]
8
9# Key-value pairs
10items = list(d.items())    # [('apple', 3), ('banana', 1), ('cherry', 5)]
11
12# Unzip into separate lists
13keys, values = zip(*d.items())
14print(keys)    # ('apple', 'banana', 'cherry')
15print(values)  # (3, 1, 5)

Getting Specific Values by Keys

python
1d = {"name": "Alice", "age": 30, "city": "Seattle", "country": "USA"}
2
3# Specific keys
4keys_wanted = ["name", "city"]
5values = [d[k] for k in keys_wanted]
6print(values)  # ['Alice', 'Seattle']
7
8# With default for missing keys
9keys_wanted = ["name", "phone", "city"]
10values = [d.get(k, "N/A") for k in keys_wanted]
11print(values)  # ['Alice', 'N/A', 'Seattle']
12
13# Using operator.itemgetter for multiple keys
14from operator import itemgetter
15get_values = itemgetter("name", "age", "city")
16print(get_values(d))  # ('Alice', 30, 'Seattle')

Filtering Values

python
1d = {"a": 10, "b": 25, "c": 5, "d": 30, "e": 15}
2
3# Values greater than 10
4filtered = [v for v in d.values() if v > 10]
5print(filtered)  # [25, 30, 15]
6
7# Keys where value meets a condition
8keys = [k for k, v in d.items() if v > 10]
9print(keys)  # ['b', 'd', 'e']
10
11# Sum, min, max of values
12print(sum(d.values()))  # 85
13print(min(d.values()))  # 5
14print(max(d.values()))  # 30

Nested Dictionary Values

python
1users = {
2    "user1": {"name": "Alice", "score": 95},
3    "user2": {"name": "Bob", "score": 87},
4    "user3": {"name": "Charlie", "score": 92},
5}
6
7# Get all names
8names = [user["name"] for user in users.values()]
9print(names)  # ['Alice', 'Bob', 'Charlie']
10
11# Get all scores
12scores = [user["score"] for user in users.values()]
13print(scores)  # [95, 87, 92]
14
15# Flatten all nested values
16all_values = [v for user in users.values() for v in user.values()]
17print(all_values)  # ['Alice', 95, 'Bob', 87, 'Charlie', 92]

Python 2 vs Python 3

python
1# Python 2: dict.values() returns a list directly
2values = d.values()  # Returns list — no need for list()
3
4# Python 3: dict.values() returns a view object
5values = d.values()  # Returns dict_values — wrap with list() if needed
6values = list(d.values())  # Explicit list
7
8# Python 2 also had dict.itervalues() for lazy iteration
9# This was removed in Python 3 because dict.values() is already lazy

Common Pitfalls

  • Forgetting that dict.values() returns a view, not a list: In Python 3, d.values() returns a dict_values object. You cannot index it (d.values()[0]) or slice it. Wrap with list() when you need list operations.
  • Assuming dictionary order is random: Since Python 3.7, dictionaries maintain insertion order. list(d.values()) returns values in the order they were inserted. In Python 3.5 and earlier, the order was undefined.
  • Modifying a dictionary while iterating over values: Adding or removing keys during for v in d.values() raises RuntimeError: dictionary changed size during iteration. Create a list copy first: for v in list(d.values()):.
  • Using list(d.values()) when iteration suffices: Converting to a list creates a copy of all values in memory. For simple iteration, for v in d.values(): uses less memory because the view does not copy data.
  • Confusing d.values() with d.items(): d.values() returns only the values. d.items() returns (key, value) tuples. If you need both keys and values, use d.items() instead of separate calls to d.keys() and d.values().

Summary

  • list(d.values()) converts dictionary values to a list
  • d.values() returns a live view object — no copy, reflects dictionary changes
  • Use the view directly for iteration, membership tests, and len() — no need for list()
  • Convert to list only when you need indexing, slicing, or an independent snapshot
  • Use sorted(d.values()) to get a sorted list without modifying the dictionary
  • Python 3.7+ guarantees insertion order for dict.values()

Course illustration
Course illustration

All Rights Reserved.