1 / 8

Lists of Dictionaries

Lists of Dictionaries. Combining Data Storage Types. {. Review of Lists. A Simple Diagram of a List. addresses. 123 King St. 0. 12 Woodbine. 1. list. }. 18 Fairfield. 2. element. 345 Johnson. 3. index (subscript).

chesna
Download Presentation

Lists of 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. Lists of Dictionaries Combining Data Storage Types

  2. { Review of Lists A Simple Diagram of a List addresses 123 King St. 0 12 Woodbine 1 list } 18 Fairfield 2 element 345 Johnson 3 index (subscript)

  3. For example, given our list on the left, the following Python command: addresses [3] = "345 Johnson“ • the name of the list is addresses • the element is addresses [3] -> pronounced “addresses sub 3” • the subscript or index is 3 • the data assigned to this element is 345 Johnson addresses 123 King St. 0 12 Woodbine 1 18 Fairfield 2 345 Johnson 3

  4. Review of Dictionaries • Remember that dictionaries are defined as a series of key:data pairs. • This means thatindividual pieces of data are accessed using a key instead of their relative position in the dictionary. A Simple Diagram of a Dictionary address • The name of the dictionary is address and it contains three keys:data pairs: • Number -> 18 • Street -> Fairfield Boulevard • City -> Amherstview

  5. Lists of Dictionaries • A list contains a number elements identified with a subscript (index) which contain data. • Instead of one piece of data, each element in a list can contain a dictionary containing multiple key:value pairs. manyAddresses 0 1 2 3

  6. For example, given our list on the previous slide, the following Python command: addresses [2] [“Street”] = “Carter Crescent“ • the name of the list is addresses • the element is addresses [2] -> pronounced “addresses sub 2” • the subscript or index is 2 • the data assigned to the addresses [2] a dictionary with three keys. • the key (field) of the dictionary in the element is “Street” • the data assigned to the key Street in the element addresses [2] is Carter Crescent

  7. Program to Create aLists of Dictionaries manyAddresses = [ ] # Creates an empty list foriinrange (0, 4): singleAddress = {} # Creates an empty dictionary # Must be in the loop # Allows the user to input data into the dictionary singleAddress ["Name"] = input ("Enter street number") singleAddress ["Street"] = input ("Enter street name") singleAddress ["City"] = input ("Enter the city") # Adds the dictionary to the list manyAddresses.append(singleAddress)

  8. How the Program Works singleAddress manyAddresses 0 1 2 3

More Related