1 / 32

More on Dictionaries

More on Dictionaries. CSE 1310 – Introduction to Computers and Programming Christopher Conly University of Texas at Arlington. Legal Keys. No duplicate keys Cannot have multiple entries for a key. Legal Keys. No duplicate keys Cannot have multiple entries for a key

luke
Download Presentation

More on Dictionaries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Dictionaries CSE 1310 – Introduction to Computers and Programming Christopher Conly University of Texas at Arlington

  2. Legal Keys • No duplicate keys • Cannot have multiple entries for a key

  3. Legal Keys • No duplicate keys • Cannot have multiple entries for a key • Keys must be immutable types

  4. Legal Keys • No duplicate keys • Cannot have multiple entries for a key • Keys must be immutable types • Strings • Integers • Tuples • Mix and match is okay

  5. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Outer dictionary brackets?

  6. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Outer dictionary brackets

  7. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Outer dictionary keys?

  8. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Outer dictionary keys

  9. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Outer dictionary values?

  10. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Outer dictionary values

  11. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Inner dictionary brackets?

  12. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Inner dictionary brackets

  13. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Inner dictionary keys?

  14. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Inner dictionary keys

  15. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Inner dictionary values?

  16. Nesting Dictionaries • A dictionary with dictionaries as values my_dict = {123: {'name': 'John', 'age': 50}, 456: {'name': 'Mary', 'age': 30}} • Inner dictionary values

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

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

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

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

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

  22. 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?

  23. 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 access the name value?

  24. 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 access the name value?

  25. 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 access the name value?

  26. 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 access the name value?

  27. 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’?

  28. 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’?

  29. 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’?

  30. 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’?

  31. 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’?

  32. 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’?

More Related