Dictionaries in Python are used to store information that comes in key-value pairs. A dictionary contains a key and its value.
Each country ( key) has its capital (value), and in this context, the different countries can be a key, whereas their respective capitals are their values.
Let’s take a look at the code below:
countries={‘Ghana’: ’Accra’}
print(countries)
We create a dictionary named countries
, where Ghana
is the key, and Accra
is the value.
{'Ghana': 'Accra'}
The code returns a dictionary as expected.
countries={"Ghana": "Accra", "China": "Beijing"}# using the del keyworddel countries["China"]print(countries)
countries={"Ghana": "Accra", "China": "Beijing"}# using the clear() methodcountries.clear()print(countries)
countries={"Ghana": "Accra", "China": "Beijing"}# using the pop() methodcountries.pop("China")print(countries)
countries={"Ghana": "Accra", "China": "Beijing"}# using the clear() methodcountries.popitem()print(countries)
Free Resources