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

44 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
  7. film said:

    As soon as I detected this website I went on reddit to share some of the love with them. Livy Nathanael Pedersen

    November 28, 2020
    Reply
  8. film said:

    Soccer is a contact sport, so you should look for jerseys that will withstand the constant abuse. Inesita Andreas Yvor

    November 29, 2020
    Reply
  9. film said:

    Muchos Gracias for your post. Much thanks again. Cool. Tonie Chrissy Fae

    November 30, 2020
    Reply
  10. film said:

    Every weekend i used to visit this site, as i want enjoyment, since this this site conations truly fastidious funny data too. Sybyl Magnum Kindig

    December 2, 2020
    Reply
  11. erotik said:

    One of our guests just lately suggested the following website. Wini Tobit Mirabel

    December 7, 2020
    Reply
  12. porno said:

    I really love your site.. Excellent colors & theme. Lettie Andreas Myranda

    December 9, 2020
    Reply
  13. sikis izle said:

    There is perceptibly a bunch to identify about this. I assume you made various nice points in features also. Hatty Enos Olympie

    December 9, 2020
    Reply
  14. erotik said:

    I am sure this post has touched all the internet people, its really really fastidious article on building up new website. Sashenka Herculie Statis

    December 9, 2020
    Reply
  15. erotik izle said:

    I like reading a post that can make men and women think. Also, many thanks for permitting me to comment. Janka Maurizio Cristie

    December 9, 2020
    Reply
  16. sikis izle said:

    Simply wanna comment that you have a very decent website , I love the style it really stands out. Salome Jedediah Nelia

    December 9, 2020
    Reply
  17. Tyson said:

    Hi there to all, it’s truly a pleasant for me to pay a visit this web site, it consists of precious Information.

    Feel free to visit my page: JerryDMoring

    December 10, 2020
    Reply
  18. dublaj izle said:

    You completed a number of nice points there. I did a search on the issue and found nearly all persons will consent with your blog. Alejandrina Armando Swift

    December 10, 2020
    Reply
  19. Jimmy said:

    Your thing is actually unique when compared with other people I’ve read stuff
    from. I appreciate you for posting when you’ve got the chance, Guess I will just
    book mark this site.

    my webpage :: TerrieDSwick

    December 29, 2020
    Reply
  20. Janelle said:

    Hey! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your posts.
    Can you suggest any other blogs/websites/forums that cover the same
    topics? Thanks a ton!

    My site: Circle Labs CBD Review

    January 4, 2021
    Reply
  21. Cecilia said:

    Neat blog! Can be your theme personalized or have you download it from somewhere?
    A theme like yours with just a few simple tweeks would really make
    my blog jump out. Please let me know that you got your
    design. With thanks

    Also visit my web site; travelers notebook 2020

    January 4, 2021
    Reply
  22. Lucio said:

    If you want to get much from this post then you have to
    apply such methods to your won weblog.

    Take a look at my web site – domino99

    January 5, 2021
    Reply
  23. atmospheric water generator price said:

    It’s an awesome piece of writing in favor of all the online visitors; they will take advantage from it I am sure.|
    atmospheric water generator price http://xsle.net/4c66n

    January 6, 2021
    Reply
  24. Britt said:

    We are a bunch of volunteers and opening a brand new scheme in our community.
    Your web site offered us with valuable information to
    paintings on. You have done a formidable job and our whole group might be thankful to you.

    Review my web-site: Bio Wellness X CBD Review

    January 7, 2021
    Reply
  25. Richie said:

    It’s hard to find educated people about this topic, but you look like you
    know what you’re speaking about! Thanks

    My webpage; filofax finsbury

    January 10, 2021
    Reply
  26. ucretsiz said:

    I really enjoy the blog post. Really thank you! Keep writing. Clarita Patsy Coop

    January 12, 2021
    Reply
  27. ucretsiz said:

    You got a very great website, Gladiola I observed it through yahoo. Tallia Gabi Kenwood

    January 13, 2021
    Reply
  28. bedava said:

    You should be a part of a contest for one of the most useful blogs on the internet. Truda Wolfgang Haswell

    January 13, 2021
    Reply
  29. full hd izle said:

    I pay a visit daily a few web sites and blogs to read content, however this webpage offers feature based content. Kirsti Farrel Roth

    January 13, 2021
    Reply
  30. hd film izle said:

    Well I really enjoyed reading it. This subject offered by you is very constructive for correct planning. Marlo Brade Daniela

    January 14, 2021
    Reply
  31. filmi full izle said:

    Although web sites we backlink to beneath are considerably not connected to ours, we feel they are in fact worth a go as a result of, so have a look. Debbi Millard Kluge

    January 14, 2021
    Reply
  32. izle said:

    This paragraph is really a pleasant one it assists new net users, who are wishing for blogging. Arleta Dill Bullion

    January 17, 2021
    Reply
  33. film izle said:

    If some one wants expert view about running a blog then i recommend him/her to go to see this webpage, Keep up the good work. Felicity Elliott Rockie

    January 17, 2021
    Reply
  34. hd film izle said:

    Way cool! Some extremely valid points! I appreciate you writing this post and also the rest of the website is really good. Marsha Franky Gifferd

    January 17, 2021
    Reply
  35. izle said:

    There is certainly a great deal to learn about this issue. I love all of the points you made. Krissie Wilhelm Ash

    January 18, 2021
    Reply
  36. Latisha said:

    Appreciate the recommendation. Let me try it out.

    Feel free to surf to my webpage :: top e-juice reviews

    January 18, 2021
    Reply
  37. full hd izle said:

    You made some good points there. I looked on the net to learn more about the issue and found most people will go along with your views on this site. Delinda Maddie Gerta

    January 18, 2021
    Reply
  38. yabanci dizi said:

    You have remarked very interesting details! ps nice web site. Rheba Manfred Hildy

    January 21, 2021
    Reply

Leave a Reply

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