Convert Dictionary Values List Python

Python programming learners can collect complete information about converting dictionary values to a list in python from this tutorial. Make use of this Convert Dictionary Values List Python Tutorial and acquire how values of the dictionary can convert into a list using a python programming language with Examples. Before going to understand just go through these available links and had a quick reference about the topics related to converting python dictionary to a list.

This Tutorial of Convert Python Dictionary Values to a List contains the below modules: 

Convert Dictionary Values to a List in Python

In this section, learners can focus on learning about a dictionary in python, it is a built-in data type that can be used to store data in a way that’s different from lists or arrays. Dictionaries aren’t sequences, so they can’t be indexed by a range of numbers, instead, they’re indexed by a series of keys.

At the time of learning dictionaries, it’s helpful to think of dictionary data as unordered key: value pairs, with the keys needing to be unique within a single dictionary. You can create a dictionary easily within a pair of curly braces. Fill the braces with data with values in the key: value format, separating your key: value pairs with commas, and you’ve got yourself a dictionary.

See the code below for an example of what a dictionary looks like and how it should be formatted:

example = {'key1': value1, 'key2': value2, 'key3': value3}

If your keys are strings, it’s important to remember to put them in quotation marks.

What’s interesting about dictionaries are all the different ways that they can be used to map data. A common dictionary is an operation is to extract the values from the key: value pairs and convert them to a list, the code for which is actually quite straightforward. To see how it’s done, check out the code snippet below.

Do Check: Quick Tip Transpose Matrix Using Python

How to Convert Python Dictionary to a list?

To start, we’ll need a dictionary to work with:

food = {'pizza': 324, 'sandwich': 78, 'hot dog': 90}

Next, we’ll need the code that can be used to execute the conversion of the values from the dictionary to a list, like this:

food_list=list(data.values())
print(food_list)

That’s it. The output of the code above should be the data values of the key: value pairs from your dictionary, in the exact order that they appear in your dictionary. So your output should look something like this:

324, 78, 90

You can use this code snippet to print the values of the key: value pairs of any dictionary as a list.

Convert all values of dictionary to list using list()

A dictionary class offers a function values(), that gives an iterable sequence of all values of dictionary in python. Also, we may move this iterable sequence(dict_values) to the list() function, then it returns a list containing all dictionary values. For instance,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    "why": 89,
    "Hi": 51,
    "How": 79
}
# Convert all values in dictionary to a list
list_of_values = list(word_freq.values())
print(list_of_values)

Output: 

[56, 23, 43, 78, 89, 51, 79]

Now, we have created a list of all values of the dictionary.

Also Refer: Use Python Multiply Strings

Convert values of dictionary to list using List Comprehension

In Python, dictionary class provides a function items(), which returns an iterable sequence (dict_items) of all key-value pairs of dictionary. By using the list comprehension, we can iterate over this sequence containing all items of the dictionary and convert the dictionary values into a list of values. For instance,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    "why": 89,
    "Hi": 51,
    "How": 79
}
# Convert all values in dictionary to a list
list_of_values = [  value 
                    for key, value in word_freq.items()]
print(list_of_values)

Result: 

[56, 23, 43, 78, 89, 51, 79]

From the above examples, you have learnt how to convert all values of dictionary to list. However, if we want to know how to convert only specific values from a given dictionary to list? then see the below module and learn how to do that easily.

Convert specific values of dictionary to list

Let’s assume that we require specific dictionary values to list. So that, we have to choose only those values of dictionary whose key is a string of length 4 or more and create a list of those values only. To attain that, we will use if condition while iterating over pairs of dictionary and select only those values where condition returns the True. For illustration,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78,
    "why": 89,
    "Hi": 51,
    "How": 79
}
# Convert dictionary values to list where key
# is a string of length greater than 4
selected_values = [ value 
                    for key, value in word_freq.items()
                    if len(key) >= 4]
print(selected_values)

Output:

[56, 43, 78]

Hence, we have converted only those specific values to list, where size of the key string is greater than 4.

Leave a Reply

Your email address will not be published.