32
loading...
This website collects cookies to deliver better user experience
{}
pythondictionary = {1: 'python', 2: 'data science', 'third': 'JavaScript'}
**
and then add them to a dictionary. Here is an example:a = {'a':1}
b = {'b':2}
c = {**a, **b}
print(c)
# this will return:
# {'a': 1, 'b': 2}
clear()
pythondictionary = { "a":1, "b":2, "c":3 }
print(pythondictionary.clear())
# this will return:
# None
copy()
will create a shallow copy of the original dictionary.pythondictionary = { "a":1, "b":2, "c":3 }
c = pythondictionary.copy()
print(c)
# this will return:
# { "a":1, "b":2, "c":3 }
fromkeys()
lets you create a dictionary from a set of keys stored in a list or tuple. For example:keys = ("a", "b", "c")
value = [1, 2, 3]
d = dict.fromkeys(keys, value)
print(d)
# this will return:
# {'a': [1, 2, 3], 'b': [1, 2, 3], 'c': [1, 2, 3]}
[]
like this:d = { "a":1, "b":2, "c":3 }
print(d["a"])
# this will return:
# 1
get()
function. This makes the action explicit. If the attribute doesn’t exist, it will return None
. If you used []
instead of get()
and the value doesn’t exist, you will get a KeyError
instead.d = { "a":1, "b":2, "c":3 }
print(d.get("a"))
# this will return:
# 1
undefined
with get()
. Here is an example:d = { "a":1, "b":2, "c":3 }
print(d.get("f", "undefined))
# this will return:
# undefined
item()
will return a dictionary’s key and value pairs as a list of tuples. Here is an example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
result = car.items()
print(result)
# this will return:
# dict_items([('make', 'Mitsubishi'), ('model', 'Lancer'), ('year', 2007), ('color', 'silver')])
for item in result:
print(item)
# this will return:
# ('make', 'Mitsubishi')
# ('model', 'Lancer')
# ('year', 2007)
# ('color', 'silver')
keys()
if you only need the keys inside a dictionary. For example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
for key in car.keys():
print(key)
# this will return:
# make
# model
# year
# color
values()
. For example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
for value in car.values():
print(value)
# this will return:
# Mitsubishi
# Lancer
# 2007
# silver
pop()
. For example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
car.pop("year")
print(car)
# this will return:
# {'make': 'Mitsubishi', 'model': 'Lancer', 'color': 'silver'}
popitem()
. For example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
car.popitem()
# this will return:
# ('color', 'silver')
print(car)
# this will return:
# {'make': 'Mitsubishi', 'model': 'Lancer', 'year': 2007}
None
by default. However, you can set the return value through setdefault()
. For example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
print(car.setdefault("origin", "undefined"))
print(car.setdefault("model", "undefined"))
# this will return:
# undefined
# followed by (because the key exists):
# Lancer
update()
. For example:car = {
"make":"Mitsubishi",
"model":"Lancer",
"year":2007,
"color":"silver"
}
car.update({"origin":"Japan"})
print(car)
# this will return:
# {'make': 'Mitsubishi', 'model': 'Lancer', 'year': 2007, 'origin': 'Japan'}