You are on page 1of 33

Nesting

CHAPTER 6: DICTIONARY
DICTIONARY
can store an almost limitless amount of information
A dictionary in Python is a collection of key-
value pairs. Each key is connected to a value, and
you can use a key to access the value associated
with that key. A key’s value can be a number, a
string, a list, or even another dictionary.
In fact, you can use any object that you
can create in Python as a value in a
dictionary.
KEY : VALUE
DICTIONARY
can store an almost limitless amount of information
A dictionary in Python is a collection of key-
value pairs. Each key is connected to a value, and
you can use a key to access the value associated
with that key. A key’s value can be a number, a
string, a list, or even another dictionary.
In fact, you can use any object that you
can create in Python as a value in a  You can list chess moves.  You can list your
dictionary.
favorite
motorcycles.

 You can define different  You can do your


terms. own game.
TAKE
NOTE:{}
o A dictionary is wrapped in braces,
with key-value pairs inside.
o Looping is essentially useful in a
dictionary to reduce codes.
o Don’t forget the f-strings before adding
statements.
o Best used in listing information.
o Simplicity is the key.
NESTING
Stores multiple dictionaries in a
list, or a list of items as a value in a
dictionary. You can nest dictionaries
Chapter
inside a 6:list,
Dictionary
a list of items inside a
dictionary, or even a dictionary inside
another dictionary.

Nesting is a powerful feature?


Let’s see !!
Things Table of
to Contents
Learn: 01 A List of Dictionaries
Nesting Dictionaries inside a list

A List in a Dictionary
02 Listing items inside a Dictionary

Dictionary in a
03 Dictionary
Creating Dictionary inside another
Dictionary
1ST TOPIC:
A List of
Dictionaries
Dictionary

List of fried chickens


Here, we store each of these
dictionaries in a list called
friedchickens.
We now LOOP (for _ in _) through the
list, then we print out each fried
chickens.
We use the method, range() to create an
army of Man-eating Fried Chickens.

* The range() here returns a series of


numbers.
Create an information for
our new chicken prototype.
Each time the loop runs we create a new alien and then
append each new alien to the list aliens.

* The append() here puts the value


on our empty monster_chicken list.
Here, we slice the list to print the first five Man-
Eating Fried Chickens.

* The slice ([ : 5 ]) only prints the first five chickens.

ICYMI: * If the slice looks like this ([ 5 : ]), Python returns all items
from the sixth item through the end of the list.
We print the length of the list to
prove we’ve actually generated
an army of Man-eating Fried
Chicken.
In our program, there would be time where it changes
its variant. Hence, we can use a for loop and an if
statement to change the variant of this chicken.

EXPAND THE LOOP??


NO PROBLEM!
By adding the method elif(), we could expand the loop
that turns another chicken’s variant from honey butter
into cheddar sour cream, the skill to healing, and its
code number to 300.
2ND TOPIC:
A List in a
Dictionaries
List of siomai flavors in our
siomai dictionary.
First, we’ll start with a dictionary holding the information
of our ordered siomai.

*KEYS: ‘process’ & ‘flavors’


*VALUES: the associated values in a string after the keys.
Usually, clerks would
summarize the order
beforehand.
To access the list of flavors, we use the key ‘flavors’
and Python grabs the list of flavors from the
dictionary.
+
The sign assigned here is the same as having
{ to the values associated
now since it still refers
in the ‘flavors’ key.
}
Some people have one favorite language and others have
multiple favorites.

The value associated with each name is now a


What will you conclude on keys having multiple
LIST
values?
} ICYMI:
The method .items() tells Python to loop through each key-
value pair in the dictionary. The result would be shown on the
left:

we use the variable name animes to hold each value from the
dictionary
Inside the main dictionary loop, we use another for loop to
run through each person’s list of favorite animes.
A
Dictionar
y in a
Dictionar
y
3RD
TOPIC:
GENTLE REMINDER:
Nesting a Dictionary in a Dictionary is actually quite
complex if and only if you’re not paying much attention on
your coding.
Here, we start with an actual data from our fellas and the
following are their responses.

* As you can see, user dictionary encloses another dictionaries,


copy cat & anaknipapa.
* We also store informations about each user and let’s see how we
loop it.
We try printing here the keys assigned on user dictionary and
the data shown below is the result.

*username designate for keys Copy cat & anaknipapa.


*user_info designate for values under Copy cat & anaknipapa.
We start accessing the inner dictionary. The variable
user_info, which contains the dictionary of user information,
has three keys: 'first', 'last', and 'location'.

We use each key to generate a neatly formatted full name


and location for each person, and then print a summary of
what we know about each user.
THANK YOU!!
Videos for Exercises to follow…

You might also like