• Dictionaries using key-value pair for the elements

  • Example of a Dictionary

animals = {
  	"marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer",
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

a dictionary example

  • I can use dir function to list the built-in methods and functions I can use to work with Dictionaries
animals = {
		   "marshmallow":"a black dog who likes to bark at things", 
           "rudolph":"the very shiny red-nosed reindeer",
           "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
}

print(dir(animals))

Built-in functions

  • I can use keys method to see all the keys in a dictionary
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

print(animals.keys())

all keys

  • Similarly I can also use values method to see all the values in a dictionary
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

print(animals.values())

all values

  • To see both keys and values I can use items method
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

print(animals.items())

dictionary items

  • I can access a certain value by using its key
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

print(animals)
print("\n")

print(animals["rudolph"])
print("\n")

print(f"Who is Doraemon? \nDoraemon is {animals['doraemon']}")

work with dictionary

  • I can also use get() function
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

print(animals.get('marshmallow'))

using get

  • To add an item to a dictionary I can do like so
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

animals["sonic"] = "a blue hedgehog who runs extremely fast"

print(animals)

add key-value

  • To change the value of an item in a dictionary I can do like so
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

animals['doraemon'] = "a cat who speaks Japanese fluently"

print(animals)

change value

  • To iterate through a dictionary I can use For loop
animals = {
    "marshmallow":"a black dog who likes to bark at things", 
    "rudolph":"the very shiny red-nosed reindeer", 
    "doraemon":"a cat with a pocket to access very advanced technological stuffs from the future"
    }

for key, val in animals.items():
    print(key,val)

dict_for-loop