Python dictionary multiple values [6+ examples]

In this Python tutorial, we will study what Python dictionary multiple values are, how to create one, and how we can manipulate it according to our needs. We are using some illustrative examples in Python to understand these all concepts. Moreover, we will also cover these topics in the article:

  • Python dictionary multiple values for one key
  • Python dictionary append multiple values
  • Python dictionary multiple values for a key
  • Python dictionary update multiple values
  • Python dict remove multiple values
  • Python list to dictionary multiple values
  • Python sort list of dictionary by multiple values

Python dictionary multiple values

Python dictionary with multiple values means a dictionary in which the keys of a dictionary are associated with multiple values of any data type in Python.

In this section, we will discuss how to create a dictionary with multiple values in Python. To perform this particular task, we are going to use the dict.values() method.

In Python, the dict.values() method returns all the values that are available in the Python dictionary and stores them in a list of dict_values.

Syntax:

Let’s have a look at the syntax and understand how the dict.values() function works:

dictionary.values()

Example: Let’s take an example and check how to create a dictionary with multiple values.

new_dictionary={'U.S.A':[12,23,45],
                'Japan':[16,89,79],
                 'Germany':[45,89,79]}

result=new_dictionary.values()
print("Dictionary values:",result)                             

Output: In the above code, we have created a Python dictionary named ‘new_dictionary’ that contains elements in the form of key-value pairs. In this example, we have assigned multiple values to each key in the dictionary. After that, we used the dict.values() method and it will extract only values from a given Python dictionary.

Dictionary values: dict_values([[12, 23, 45], [16, 89, 79], [45, 89, 79]])

Here is the Screenshot of the following given code:

python dictionary with multiple values

This way we can create a Python dictionary multiple values.

Python dictionary multiple values for one key

  • In this Program, we will discuss how to get multiple values for one key in a Python dictionary.
  • To do this task, we are going to select a specific key and it gives the value of that key.
  • In this example, we will create a Python dictionary and then declare a variable ‘result’ in which we will assign the selected item from the dictionary in Python.

Example:

new_dictionary={'France':[67,145,789,134],
                'Japan':[745,109,897,345],
                 'England':[478,390,289,189]}

result=new_dictionary['Japan']
print("values of japan key:",result)

Output: Here is the implementation of the following given code.

values of japan key: [745, 109, 897, 345]
python dictionary multiple values per key

As you can see in the Screenshot the output displays the multiple values of the ‘Japan‘ key.

Python dictionary append multiple values

  • Here we are going to discuss how to append multiple values in the Python dictionary.
  • By using the dictionary.update() function, we can easily append the multiple values in the existing dictionary.
  • In Python, the dictionary.update() method will help the user to update the dictionary elements or if it is not present in the dictionary then it will insert the key-value pair.

Syntax:

Let’s have a look at the syntax and understand how the Python dictionary.update() function works in Python. The syntax is as follows:

dict.update([other])

Here, other : is the another key-value pair for the existing dictionary that we want to add or append to it.

Example: In the above code, we have appended a list of values to a key(Micheal) in the Python dictionary within the dict.update() function. Once you will execute this code the output displays the multiple values.

new_dictionary = {'George':786,'Micheal':234,'John':467}

result=new_dictionary.update({'Micheal':[98,67,58] })
 
print("appending multiple values:",new_dictionary)

Output: Here is the Output of the following given Python code.

appending multiple values: {'George': 786, 'Micheal': [98, 67, 58], 'John': 467}
multiple values in dictionary python

Python dictionary multiple values for a key

  • In this section, we will discuss how to get the multiple values for a specific key in the Python dictionary.
  • To do this task, we are going to use the set.default() function. the set.defaultdict() is used to set the default values to the dictionary key in Python.

Example:

new_dictionary = [('Micheal', 678), ('John', 987), ('John', 345), ('Oliva', 432), ('William', 445)]

empty_dictionary = {}
for i in new_dictionary:
    empty_dictionary.setdefault(i[0],[]).append(i[1])
print(empty_dictionary)

Here is the implementation of the following code:

{'Micheal': [678], 'John': [987, 345], 'Oliva': [432], 'William': [445]}
python dictionary with multiple values for a key

Python dictionary update multiple values

  • In this section, we will learn how to update the multiple values in the Python dictionary.
  • To perform this particular task, we are going to use the dictionary comprehension method this function is used to create a dictionary and it iterates the key-value pair.
  • Within this dictionary comprehension method, we are going to use the assignment operator ‘+’ and this operand will help the user to add values in a given key.

Example:

new_dictionary = {'U.S.A': 89, 'France': 45, 'United Kingdom': 94, 'China': 345}

new_result = {new_k: new_dictionary[new_k]+2 for new_k in new_dictionary}
print("Updated multiple values in dictionary:",new_result)

Output: In the above code, we have created a dictionary that contains elements in the form of key-value pairs. After that, to update the multiple values in the dictionary, we have assigned the ‘+2’ operator and this operand will update each value in the dictionary.

Here is the execution of the following given code.

Updated multiple values in dictionary: {'U.S.A': 91, 'France': 47, 'United Kingdom': 96, 'China': 347}
how to add multiple values in dictionary python

Python dict remove multiple values

  • In this section, we will learn how to remove multiple values in a Python dictionary.
  • To perform this particular task, we are going to use dictionary comprehension and dict.items() method for removing multiple values from a dictionary. This method will always return an object of the dictionary.
  • In the dictionary comprehension method, we will set the condition new_val !=278 that indicates if the value is available in the dictionary then it will remove from the dictionary.

Example:

new_dict = {"John" : 156, "Micheal" :278, "George" : 987, "William" : 345}
  
rem_value = {new_key:new_val for new_key, new_val in new_dict.items() if new_val != 278}
rem_value2 = {new_key:new_val for new_key, new_val in new_dict.items() if new_val != 987}

print ("Remove after multiple values is :",(rem_value),(rem_value2))

Output: In the following given code, we have created a Python dictionary and then declared two variables: ‘rem_value’, ‘rem_value2’ and, within those variables, we have used the dictionary comprehension method and will remove multiple values from the dictionary in Python.

Here is the implementation of the following given code.

dictionary with multiple values python

Python list to dictionary multiple values

  • In this Program, we will learn how to convert the list into a Python dictionary with multiple values in Python.
  • To do this task, we are going to use the zip() and dict() functions. In Python the zip() function takes iterable objects as an argument like list and tuples and it always returns an iterator of tuples based on the object.
  • And then, to convert the object into a dictionary we are going to use the dict() function, and this function is used to create a dictionary in Python.

Syntax:

Here is the Syntax of the Python zip() function.

zip(*iterables)

Example: Let’s take an example and check how to convert the list into a dictionary with multiple values.

list1 = ['USA', 'Japan', 'United Kingdom']

list2 = [78, 34, 89]
new_result = dict(zip(list1, list2))
print("Convert lists into dict",new_result)

Output: In the following given code, we have created two lists ‘list1’ and ‘list2’. In the list1 we have assigned the key elements as Country_name and list2 contains multiple values. Once you will execute this code the output displays the new dictionary that contains elements in the form of key-value pairs.

Convert lists into dict {'USA': 78, 'Japan': 34, 'United Kingdom': 89}

You can refer to the below Screenshot:

how to get multiple values from dictionary in python

Python sort list of dictionary by multiple values

  • In this section, we will learn how to sort a list of dictionaries by multiple values in Python.
  • In this example, we will create a list of dictionaries and within this dictionary, there are the same key elements with different multiple values.
  • Next, we have to sort the list of dictionaries by using the sorted() function, within this function we have specified the key element and according to the values of the key elements will be sorted in ascending order.

Example:

new_dict = [{'George':456, 'William': 6934,'John':456},
            {'George':897, 'William': 918,'John':734},
            {'George':345, 'William': 1456,'John':241}]

new_output = sorted(new_dict, key=lambda d: d['William'])
print(new_output)

Output:

[{'George': 897, 'William': 918, 'John': 734}, {'George': 345, 'William': 1456, 'John': 241}, {'George': 456, 'William': 6934, 'John': 456}]

Here is the Screenshot of the following given code:

add multiple values to dictionary python

As you can see in the Screenshot the output displays the sorted list of dictionaries as per the condition.

Also, take a look at some more Python tutorials.

In this tutorial, we have learned Python dictionary multiple values using some examples in Python. Moreover, we have also covered these topics.

  • Python dictionary multiple values for one key
  • Python dictionary append multiple values
  • Python dictionary multiple values for a key
  • Python dictionary update multiple values
  • Python dict remove multiple values
  • Python list to dictionary multiple values
  • Python sort list of dictionary by multiple values