A Guide to Python Dictionary

A dictionary is a container class that holds a set of values and allows you to access the values using their keys. A key is a string that identifies an individual value. Keys must be immutable; a key can't be changed once it has been created.

Using Python dictionaries allows you to create data structures with keys and values. Dictionaries are similar to lists that are indexed by integers, but each key maps to only one value within a set of values. The values represent objects, such as strings, numbers, or other dictionaries.

We can use the Dictionary to store the values in a scenario like this

subject = {
	"Maths": 78,
	"Physics": 80,
}

Python Dictionary

Here we have listed the marks of each subject. This Dictionary has two keys and two values. Keys are stored as String and it is on the left side of the colon while values are stored on the right side of the colon

In dictionary we cant use add[],insert(), or append() method like tuples and lists. For the dictionary, we need to create a new key that will store the value that you want to store in the dictionary.

We can add a new value by this method.

dictionary_name[key] = value

Accessing values in Dictionary

dict = { 'France': 'Paris', 'United States of America': 'Washington,D.C' }
print("dict[France]:", dict['France'])

We can acces the value like this, when the code is executed the following output is generated

dict['France']: Paris

Print a dictionary

Python dictionary properties

  • Dictionary can store any arbitrary python object, either standard or user-defined objects, but for keys, it is not true.

  • More than one entry for a key is not allowed i.e here the duplicate key is not allowed. When it encounters duplicate keys, only the last assignment is recognized.

For example:

dict =  { 'France':'Rennes', 'France':'Paris' }
print("dict[France]:", dict['France'])

When you run this code it generates the following output:

dict['France']: Paris
  • Keys must be immutable in the dictionary i.e we can use strings, numbers, or tuples as the keys but like similar to the key is not allowed.

Built-in dictionary functions

Below listed are some of the built-in functions in python dictionary.

  • cmp(dict1, dict2) - this will compare both elements that are presented in the dictionary.
  • len(dict) - this will give the total length of the dictionary.
  • str(dict) - this will produce the Printable String representation of the dictionary.
  • type(variable) - this will return the types of the passed variable, if the passed variable is found in the Dictionary, then it will return a dictionary type.
  • dict.clear() - this will remove all the elements in the dictionary named dict.
  • dict.copy() - this will return the shallow copy of the dictionary dict.

Conclusion

Dictionaries, or dicts for short, are a powerful data structure in python. Dictionaries are collections of key/value pairs and come with several useful methods for accessing, creating, and updating keys and values.


Further Reading

  1. RuntimeError: Working outside of application context in flask
  2. [SOLVED] ValueError: View function did not return a response
  3. How do you fix UnboundLocalError in Python?

Monitor your software stack for free with Atatus.

Start your free trial
OR
Request a Demo

Free 14-day trial. No credit card required. Cancel anytime.

Ready to see actionable data?

Avail Atatus features for 14 days free-trial. No credit card required. Instant set-up.