Dictionary and List?


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/0/4?curriculum_id=4f89dab3d788890003000096 and https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096


I'm confused why in this code below it will resulted dictionaries like this:
{'quizzes': [88.0, 40.0, 94.0], 'tests': [75.0, 90.0], 'name': 'Lloyd', 'homework': [90.0, 97.0, 75.0, 92.0]}
{'quizzes': [82.0, 83.0, 91.0], 'tests': [89.0, 97.0], 'name': 'Alice', 'homework': [100.0, 92.0, 98.0, 100.0]}
{'quizzes': [0.0, 75.0, 78.0], 'tests': [100.0, 100.0], 'name': 'Tyler', 'homework': [0.0, 87.0, 75.0, 22.0]}
None


lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = [lloyd, alice, tyler]

for student in students:
    print student

While in the course I learned before, it resulted only the key in dictionaries (not including value)?
apple
banana
orange
pear
None

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

prices_list = sorted(prices.keys())

for key in prices_list:
    print key

#2

Dictionaries do not remember the order of insertion. Consequently when printing or iterating we cannot predict the order the members will display.

As for keys and values,

for student in students:

In the above, students is a list of dictionary identifiers (variable references). student will be one of the dictionaries.

print student

{'quizzes': [88.0, 40.0, 94.0], 'tests': [75.0, 90.0], 'name': 'Lloyd', 'homework': [90.0, 97.0, 75.0, 92.0]}

The above dictionary is the value associated with the key, lloyd.

print student['name']    # Lloyd

#3

and why this code

for key in prices_list:
    print key

resulted only the keys, not the whole dictionaries?


#4

Your variable prices_list is a list, not a dictionary. You are actually printing values of each element, which are only keys.

['apple', 'banana', 'orange', 'pear']

#5

what is the differences between list and dictionary? because their format is the same from what I understood (dictionary/list = {}),

and

how would I change this code

for key in prices_list:
    print key

So that it will print the whole dictionary (key + value)?
the result I expected is like { "banana": 4,"apple": 2, "orange": 1.5, "pear": 3}
{ "banana": 6, "apple": 0, "orange": 32, "pear": 15}

btw I also change the code into

for key in stock:
    print key

it also resulted the same


#6
for key in prices_list:
    print key, prices[key]

#7

can you also please answer this?


#8

how would I change this code

for student in students:

so that it only prints out the key?


#9

for student in students:
    for key in student:
        print key

Recall that student is a dictionary identifier. hence the nested loop to get at the keys of the associated dictionary.


#10

What is dictionary identifier?


#11
my_dict = {}

my_dict is the identifier (variable) {} is the dictionary. They are called thus owing to the fact that only one object can exist under any name. Each variable has a unique name, hence, identity.

Dictionaries and Lists are both Python object classes, so,

isinstance(my_dict, dict)    # True

This tells us that my_dict is an instance of the dict class.


#12

Ah, the heat in my head calms down. Thank you mtf!


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.