This comprehensive guide covers everything you need to know about Python dictionary methods, from the basics to advanced techniques. This tutorial will show you 9 ways to create a Python dictionary and how to work with it using the built-in methods provided by Python. Python dictionary methods are essential for efficiently managing your data and building powerful applications.
In Python, a dictionary is one of the core data structures. It is a sequence of key-value pairs separated by commas and surrounded by curly braces.
If you’re familiar with JavaScript, Python dictionaries are like JavaScript objects.
Python provides more than 10 methods for working with dictionaries.
In this article, I will show you how to create a dictionary in Python and work with it using those methods.
get()
Dictionary Methoditems()
Dictionary Methodkeys()
Dictionary Methodvalues()
Dictionary Methodpop()
Dictionary Methodpopitem()
Dictionary Methodupdate()
Dictionary Methodcopy()
Dictionary Methodclear()
Dictionary MethodTo create a dictionary, you open up a curly brace and put the data in a key-value pair separated by commas.
The basic syntax of a dictionary looks like this:
demo_dict = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Note that the values can be of any data type and can be duplicated, but the key must not be duplicated. If the keys are duplicated, you will get an invalid syntax error.
I will be working with the dictionary below to show you how the dictionary methods work:
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
get()
Dictionary MethodThe get method returns the value of a specified key.
In the code below, I was able to get the founder of freeCodeCamp by passing the founder
key inside the get()
method:
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
founder = first_dict.get("founder")
print(founder)
# Output: Quincy Larson
items()
Dictionary MethodThe items()
method returns all the entries of the dictionary in a list. In the list is a tuple representing each of the items.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
items = first_dict.items()
print(items)
# Output: dict_items([('name', 'freeCodeCamp'), ('founder', 'Quincy Larson'), ('type', 'charity'), ('age', 8), ('price', 'free'), ('work-style', 'remote')])
keys()
Dictionary MethodThe keys()
returns all the keys in the dictionary. It returns the keys in a tuple – another Python data structure.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
dict_keys = first_dict.keys()
print(dict_keys)
# Output: dict_keys(['name', 'founder', 'type', 'age', 'price', 'work-style'])
values()
Dictionary MethodThe values method accesses all the values in a dictionary. Like the keys()
method, it returns the values in a tuple.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
dict_values = first_dict.values()
print(dict_values)
# Output: dict_values(['freeCodeCamp', 'Quincy Larson', 'charity', 8, 'free', 'remote'])
pop()
Dictionary MethodThe pop()
method removes a key-value pair from the dictionary. To make it work, you need to specify the key inside its parentheses.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
first_dict.pop("work-style")
print(first_dict)
# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free'}
You can see the work-style
key and its value have been removed from the dictionary.
popitem()
Dictionary MethodThe popitem()
method works like the pop()
method. The difference is that it removes the last item in the dictionary.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
first_dict.popitem()
print(first_dict)
# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free'}
You can see that the last key-value pair ("work-style": "remote") has been removed from the dictionary.
update()
Dictionary MethodThe update()
method adds an item to the dictionary. You have to specify both the key and value inside its braces and surround it with curly braces.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
first_dict.update({"Editor": "Abbey Rennemeyer"})
print(first_dict)
# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free', 'work-style': 'remote', 'Editor': 'Abbey Rennemeyer'}
The new entry has been added to the dictionary.
copy()
Dictionary MethodThe copy()
method does what its name implies – it copies the dictionary into the variable specified.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
second_dict = first_dict.copy()
print(second_dict)
# Output: {'name': 'freeCodeCamp', 'founder': 'Quincy Larson', 'type': 'charity', 'age': 8, 'price': 'free', 'work-style': 'remote'}
clear()
Dictionary MethodThe clear method removes all the entries in the dictionary.
first_dict = {
"name": "freeCodeCamp",
"founder": "Quincy Larson",
"type": "charity",
"age": 8,
"price": "free",
"work-style": "remote",
}
first_dict.clear()
print(first_dict)
# Output: {}
In this article, you learned how to create a Python dictionary and how to work with it using the built-in methods provided by Python.
If you find the article helpful, don’t hesitate to share it with friends and family.
Keep coding :)
Source: https://www.freecodecamp.org
#python