Python Dictionary for beginners with example

A python dictionary is a collection which is unordered, changeable and indexed. In Python dictionary are written with curly brackets, and they have keys and values.

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

Create and print a dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

Accessing Values in Dictionary

To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. Following is a simple example −

x = thisdict["model"]

There is also a method called get() that will give you the same result:

x = thisdict.get("model")

Python Dictionary – Updating Dictionary

You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["year"] = 2018

Adding Items to Python Dictionary

Adding an item to the dictionary is done by using a new index key and assigning a value to it:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["color"] = "red"
print(thisdict)

Removing Items

There are several methods to remove items from a dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model")
print(thisdict)

The del keyword removes the item with the specified key name:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict["model"]
print(thisdict)

The del keyword can also delete the dictionary completely:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.

The clear() method empties the dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.clear()
print(thisdict)

Make a copy of a dictionary with the copy() method:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
mydict = thisdict.copy()
print(mydict)

Another way to make a copy is to use the built-in function dict().

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
mydict = dict(thisdict)
print(mydict)

Nested Dictionaries

A dictionary can also contain many dictionaries, this is called nested dictionaries.

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}

The dict() Constructor

It is also possible to use the dict() constructor to make a new dictionary:

thisdict = dict(brand="Ford", model="Mustang", year=1964)
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)

Loop Through a Python Dictionary

You can loop through a dictionary by using a for loop.

When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

for x in thisdict:
  print(x)

Recommended Posts

Angular Guard

6 Comments

  1. film said:

    I think this is a real great blog article. Really Cool. Gladi Niki Abramson

    November 23, 2020
    Reply
  2. film said:

    You are my inspiration , I have few blogs and often run out from to brand. Fernanda Kim Medovich

    November 23, 2020
    Reply
  3. film said:

    The data talked about within the report are some of the top obtainable. Bridgette Dominik Ritz

    November 24, 2020
    Reply
  4. film said:

    Hi there, I enjoy reading all of your article. I like to write a little comment to support you. Corliss Roddie Riti

    November 26, 2020
    Reply
  5. film said:

    Way cool! Some extremely valid points! I appreciate you penning this post and also the rest of the site is also really good. Illa Siffre Hennahane

    November 26, 2020
    Reply
  6. film said:

    Give both feet your attention instead of only the stronger one. Kasey Louie Anni

    November 27, 2020
    Reply

Leave a Reply

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