Sai A Sai A
Updated date Aug 23, 2023
In this blog, we will learn multiple techniques for converting a dictionary's values into a list in Python. Explore methods like loops, list comprehensions, built-in functions, and more.

Introduction:

Dictionaries are a fundamental data structure in Python, allowing us to store and retrieve data in a key-value format. Often, there arises a need to extract just the values from a dictionary and store them in a list. In this blog, we will explore various methods to achieve this task, each offering its own advantages and use cases. 

Method 1: Using a Loop

The first approach is to use a loop, iterating through the dictionary's values and appending them to a list. Here's a Python program demonstrating this method:

def dict_to_values_list(dictionary):
    values_list = []
    for value in dictionary.values():
        values_list.append(value)
    return values_list

# Example dictionary
sample_dict = {'a': 1, 'b': 2, 'c': 3}
values = dict_to_values_list(sample_dict)
print(values)

Output:

[1, 2, 3]

In this method, we define a function dict_to_values_list() that takes a dictionary as an argument. We then use a for loop to iterate through the values of the dictionary using the values() method. For each value, we append it to the values_list. Finally, the function returns the list containing all the values.

Method 2: Using List Comprehension

List comprehension is a concise and Pythonic way to achieve the same result. Let's see how it's done:

def dict_to_values_list(dictionary):
    values_list = [value for value in dictionary.values()]
    return values_list

# Example dictionary
sample_dict = {'a': 1, 'b': 2, 'c': 3}
values = dict_to_values_list(sample_dict)
print(values)

Output:

[1, 2, 3]

Here, we use a list comprehension to create the values_list directly. The list comprehension iterates through the values of the dictionary and adds each value to the list. This method is more concise and is favored by Python programmers for its readability.

Method 3: Using the map() Function

The map() function applies a given function to all items in an input list. We can leverage this function to extract the values from the dictionary. Let's take a look:

def dict_to_values_list(dictionary):
    values_list = list(map(lambda x: x[1], dictionary.items()))
    return values_list

# Example dictionary
sample_dict = {'a': 1, 'b': 2, 'c': 3}
values = dict_to_values_list(sample_dict)
print(values)

Output:

[1, 2, 3]

In this method, we use the items() method to get a list of key-value pairs from the dictionary. The map() function applies a lambda function to each pair and extracts the second element (the value). Finally, we convert the map object to a list.

Method 4: Using the values() Method

Interestingly, the values() method itself returns a view of all the values in the dictionary. We can directly convert this view to a list:

def dict_to_values_list(dictionary):
    values_list = list(dictionary.values())
    return values_list

# Example dictionary
sample_dict = {'a': 1, 'b': 2, 'c': 3}
values = dict_to_values_list(sample_dict)
print(values)

Output:

[1, 2, 3]

Here, we use the values() method to get the view of dictionary values and then convert this view into a list using the list() constructor.

Conclusion:

In this blog, we explored various methods to convert a dictionary to a list of values in Python. We started with a simple loop-based approach and progressed to more short methods like list comprehensions and built-in functions like map().

Comments (0)

There are no comments. Be the first to comment!!!