How To Create And Work With Python Dictionaries With Multiple Values?

Python dictionaries with multiple values

To continue the series of articles on Dictionary in Python, today we will show you how to create and work with Python dictionaries with multiple values. Read and practice to absorb knowledge effectively!

Creating Python dictionaries with multiple values

Here are two common ways to create a dictionary with multiple values.

Creating in the usual way

You can create a dictionary with multiple values by following the syntax below:

{ 'key_1': [value_1.1, value_1.2, ...], 'key_2': [value_2.1, value_2.2, ...], ...}

Remember, the outer wrap is always curly braces { }, and the inner values must be a data type that can hold multiple data such as list, tuple, and set.

Below is an example of how to create a dictionary with multiple values. 

# Create a dictionary in the usual way
websites = {
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

print(websites)

Output:

{
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

Using variables

In addition, you can also use the variable containing a list, a tuple, or a set to create a dictionary with multiple values. Like this:

# Create lists to add to the dictionary
learnshareit = ['javascript', 'python', 'typescript', 'reactjs']
w3schools = ['HTML', 'CSS', 'javascript', 'python']
freecodecamp = ['HTML', 'CSS', 'javascript', 'python', 'SQL']

# Create a dictionary using variables
websites = {
  'learnshareit': learnshareit,
  'w3schools': w3schools,
  'freecodecamp': freecodecamp,
}

print(websites)

Output:

{
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

The result is still as we expected, right?

Working with Python dictionaries with multiple values

Get all values ​​in a dictionary with multiple values

To do that, we will use the values() function.

Syntax:

dict.values()

Description:

This function has no parameters. It will return a view object that shows all the values ​​in a dictionary.

We will use the values() function to get all the dictionary’s values. Since the return value of values() is an unreachable object, we have to convert it to a list to access and use it. Like this:

websites = {
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

# Get all the values in the websites as a list
values = list(websites.values())
print(values)

Output:

[
  ['javascript', 'python', 'typescript', 'reactjs'],
  ['HTML', 'CSS', 'javascript', 'python'],
  ['HTML', 'CSS', 'javascript', 'python', 'SQL']
]

Combine two dictionaries with multiple values

To do that, we will use the Union | operator. This operator merges two dictionaries and ensures no duplicate values ​​in the returned result.

Let’s combine two dictionaries and get all their values!

bookmark1 = {
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

bookmark2 = {
  'udemy': ['python', 'javascript', 'typescript', 'reactjs', 'vue', 'angular'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

# Combine two dictionaries using the Union operator
websites = bookmark1 | bookmark2
print('websites:\n', websites)

# Get all their values
values = list(websites.values())
print('All values:\n', values)

Output:

websites:
{
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL'],
  'udemy': ['python', 'javascript', 'typescript', 'reactjs', 'vue', 'angular']
}
All values:
[
  ['javascript', 'python', 'typescript', 'reactjs'],
  ['HTML', 'CSS', 'javascript', 'python'],
  ['HTML', 'CSS', 'javascript', 'python', 'SQL'],
  ['python', 'javascript', 'typescript', 'reactjs', 'vue', 'angular']
]

Note: this operator is only used for Python version 3.9 and later.

Update multiple values ​​for a dictionary with multiple values

We can update multiple values ​​using another dictionary with the same keys and update() function.

The update() function will replace the duplicate keys with the value of the dictionary passed into this function. See the example below for a better understanding.

websites = {
  'learnshareit': ['javascript', 'python', 'typescript', 'reactjs'],
  'w3schools': ['HTML', 'CSS', 'javascript', 'python'],
  'freecodecamp': ['HTML', 'CSS', 'javascript', 'python', 'SQL']
}

bookmark = {
  'learnshareit': ['javascript', 'python'],
  'w3schools': ['HTML', 'CSS'],
  'freecodecamp': ['SQL', 'R']
}

# Update websites with bookmark
websites.update(bookmark)
print('websites:\n', websites)

Output:

websites:
{
    'learnshareit': ['javascript', 'python'],
    'w3schools': ['HTML', 'CSS'],
    'freecodecamp': ['SQL', 'R']
}

*From Python 3.9 and later, we can use the Union operator to update multiple values for a dictionary. The usage is the same as the way to combine the two dictionaries we mentioned above. Try it yourself.

Summary

Congratulations, you now know how to create and work with Python dictionaries with multiple values. Working with dictionaries with multiple values is similar to working with a regular dictionary. If you are familiar with regular dictionaries, you won’t have any problems with dictionaries with multiple values.

Happy coding!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *