pronounced “addresses sub 3” – the subscript or index is 3 – the data assigned to this element is 345 Johnson 123 King St. 12 Woodbine 18 Fairfield 345 Johnson addresses"> pronounced “addresses sub 3” – the subscript or index is 3 – the data assigned to this element is 345 Johnson 123 King St. 12 Woodbine 18 Fairfield 345 Johnson addresses">
Download presentation
Presentation is loading. Please wait.
Published byJames Goodwin Modified over 9 years ago
1
Lists of Dictionaries Combining Data Storage Types
2
123 King St. 12 Woodbine 18 Fairfield 345 Johnson 0 1 2 3 addresses } element { list index (subscript) Review of Lists A Simple Diagram of a List
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 123 King St. 12 Woodbine 18 Fairfield 345 Johnson 0 1 2 3 addresses
4
Review of Dictionaries Remember that dictionaries are defined as a series of key:data pairs. This means that individual pieces of data are accessed using a key instead of their relative position in the dictionary. NumberStreetCity 18Fairfield BoulevardAmherstview address The name of the dictionary is address and it contains three keys:data pairs: Number -> 18 Street -> Fairfield Boulevard City -> Amherstview A Simple Diagram of a Dictionary
5
Lists of Dictionaries 0 1 2 3 manyAddresses NumberStreetCity 1233King StreetKingston NumberStreetCity 18Fairfield BoulevardAmherstview Number Street City 423-A Carter Crescent Bath NumberStreetCity 77Dawson RoadKingston 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.
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
manyAddresses = [ ] # Creates an empty list for i in range (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) Program to Create a Lists of Dictionaries
8
0 1 2 3 manyAddresses NumberStreetCity 18Fairfield BoulevardAmherstview NumberStreetCity 1233King StreetKingston NumberStreetCity singleAddress How the Program Works
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.