In this Python tutorial, we will study Python dictionary multiple values using some examples in python. Moreover, we will also cover 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 dictionary with multiple values to dataframe
- Python dict remove multiple values
- Python list to dictionary multiple values
- Python sort list of dictionary by multiple values
Python dictionary multiple values
- In this section, we will discuss how to create a dictionary with multiple values in Python dictionary.
- To perform this particular task ,we are going to use the dict.values() method. In Python, the dict.values() method returns all the values which are available in the dictionary and stores them into a list.
Syntax:
Let’s have a look at the syntax and understand the working of dict.values() function
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)
In the above code, we have created a dictionary named ‘new_dictionary’ that contains elements in the form of key-value pairs. In this example, we have assigned multiple values with each key. After that, we have used the dict.values() method and it will extract only values from a given dictionary.
Here is the Screenshot of the following given code.
Read: Python Dictionary duplicate keys
Python dictionary multiple values for one key
- In this Program, we will discuss how to get the multiple values for one key in Python dictionary.
- To do this task, we are going to select the specific key and it gives the value of that key.
- In this example, we will create a dictionary and then declare a variable ‘result’ in which we will assign the selected item from the dictionary.
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)
Here is the implementation of the following given code.
As you can see in the Screenshot the output displays the multiple values of the ‘Japan’ key.
Read: Get First Key in dictionary Python
Python dictionary append multiple values
- Here we are going to discuss how to append multiple values in 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 the working of the Python dictionary.update() function.
dict.update([other])
Example:
new_dictionary = {'George':786,'Micheal':234,'John':467}
result=new_dictionary.update({'Micheal':[98,67,58] })
print("appending multiple values:",new_dictionary)
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.
Here is the Output of the following given code.
Read: Python dictionary increment value
Python dictionary multiple values for a key
- In this section, we will discuss how to get the multiple values for a specific key in Python dictionary.
- To do this task, we are going to use the set.default() function and it is used to set the default values to the dictionary key.
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 given code
Read: Python dictionary extend
Python dictionary update multiple values
- In this section, we will learn how to update the multiple values in 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)
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. Once you will execute this code the output displays the updated values in the dictionary.
Here is the execution of the following given code.
Read: Python Dictionary Copy with Examples
Python dictionary with multiple values to dataframe
- In this Program, we will learn how to convert a dictionary to Pandas Dataframe with multiple values in Python.
- To do this task first we will create a dictionary and assign key-value pair but multiple values will be stored in the list and it will consider as values.
- Next, to convert a dictionary into Pandas dataframe we are going to use the pd.DataFrame() syntax and within these parameters we are going to assign the dictionary ‘new_dict’ along with .T.
- In Python Pandas, the .T is used to shift columns and index in Dataframe and this ‘T’ indicates the Transpose. Next, we will use the df.reset.index() method and this method will re-order the index of the dataframe.
Syntax:
Here is the Syntax of df.reset.index() method.
DataFrame.reset_index
(
Level=None,
drop=False,
inplace=False,
col_Level=0,
col_fill=''
)
Example:
Let’s take an example and check how to convert a dictionary to a dataframe with multiple values in a Python dictionary.
Source Code:
import pandas as pd
new_dict={'George': [89, 45], 'John': [478, 945], 'Micheal': [456, 986]}
df = pd.DataFrame(new_dict).T
df.reset_index(inplace=True)
df.columns = ['Student_name','val1','val2']
print(df)
Here is the Screenshot of the following given code.
Read: How to create an empty Python dictionary
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))
In the following given code, we have created a dictionary and then declared two-variable ‘rem_value’, ‘rem_value2’ and within this variable, we have used the dictionary comprehension method and it will remove multiple values from the dictionary.
Here is the implementation of the following given code.
Read: Python convert dictionary to an array
Python list to dictionary multiple values
- In this Program, we will learn how to convert the list into the 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 return 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 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)
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.
You can refer to the below Screenshot.
Read: Python dictionary of tuples
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 and 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)
Here is the Screenshot of the following given code.
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.
- Python Dictionary of sets
- How to delete a Dictionary in Python
- Python dictionary comprehension
- Python convert dictionary to list
- Iterate through dictionary Python
- Python dictionary item() method [With Examples]
- Python dictionary contains + examples
- Python dictionary filter + Examples
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 dictionary with multiple values to dataframe
- Python dict remove multiple values
- Python list to dictionary multiple values
- Python sort list of dictionary by multiple values
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.