nested dictionaries

29
Nested Dictionaries CSE 1310 – Introduction to Computers and Programming Christopher Conly University of Texas at Arlington

Upload: kineta

Post on 06-Jan-2016

109 views

Category:

Documents


3 download

DESCRIPTION

Nested Dictionaries. CSE 1310 – Introduction to Computers and Programming Christopher Conly University of Texas at Arlington. Nesting Dictionaries. A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Nested  Dictionaries

Nested Dictionaries

CSE 1310 – Introduction to Computers and ProgrammingChristopher Conly

University of Texas at Arlington

Page 2: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Outer dictionary brackets?

Page 3: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Outer dictionary brackets

Page 4: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Outer dictionary keys?

Page 5: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Outer dictionary keys

Page 6: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Outer dictionary values?

Page 7: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Outer dictionary values

Page 8: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Inner dictionary brackets?

Page 9: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Inner dictionary brackets

Page 10: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Inner dictionary keys?

Page 11: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Inner dictionary keys

Page 12: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Inner dictionary values?

Page 13: Nested  Dictionaries

Nesting Dictionaries

• A dictionary with dictionaries as valuesmy_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}– Inner dictionary values

Page 14: Nested  Dictionaries

Accessing Nested Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}How do you access 'John'?

Page 15: Nested  Dictionaries

Accessing Nested Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}How do you access 'John'?my_dict[123]['name']

Page 16: Nested  Dictionaries

Accessing Nested Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}How do you access 'John'?my_dict[123]['name']

Page 17: Nested  Dictionaries

Accessing Nested Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}How do you access 'John'?my_dict[123]['name']

Page 18: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for item in my_dict: ...What is item?

Page 19: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for item in my_dict: ...What is item? A key.How do we access the name value?

Page 20: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for item in my_dict: name = my_dict[item]['name']How do we access the name value?

Page 21: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for item in my_dict: name = my_dict[item]['name']How do we access the name value?

Page 22: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for item in my_dict: name = my_dict[item]['name']How do we access the name value?

Page 23: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for item in my_dict: name = my_dict[item]['name']How do we access the name value?

Page 24: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for key1 in my_dict: ...

What about looping through ‘name’ and ‘age’?

Page 25: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for key1 in my_dict: for key2 in my_dict[key1]: print my_dict[key1][key2]

What about looping through ‘name’ and ‘age’?

Page 26: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for key1 in my_dict: for key2 in my_dict[key1]: print my_dict[key1][key2]

What about looping through ‘name’ and ‘age’?

Page 27: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for key1 in my_dict: for key2 in my_dict[key1]: print my_dict[key1][key2]

What about looping through ‘name’ and ‘age’?

Page 28: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for key1 in my_dict: for key2 in my_dict[key1]: print my_dict[key1][key2]

What about looping through ‘name’ and ‘age’?

Page 29: Nested  Dictionaries

Looping and Dictionaries

my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}}for key1 in my_dict: for key2 in my_dict[key1]: print my_dict[key1][key2]

What about looping through ‘name’ and ‘age’?