-
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"
}
- 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))
- 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())
- 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())
- 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())
- 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']}")
- 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'))
- 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)
- 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)
- 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)