Structured Data: Lists and Dictionaries
A list:
schools = ['Columbia University', 'NYU', 'Cooper Union']
A dictionary:
schools = {
'institution name':'Columbia University',
'student poplation':34000,
'degree types':['undergrad','grad']
}
both can be single level:
list: [0,1,2,3,4]
vs dictionary: {"a":"b", "c":"d", "e":"f"}
or nested:
list: [[1,2],3,[4,5],[6,7,8]]
vs nested dictionary: {"a":"b", "c":{"d":"e"}}
List Indexing:
schools = ['Columbia University', 'NYU', 'Cooper Union']print(schools[-1])
print(schools[0:2])
append:
schools = ['Columbia University', 'NYU', 'Cooper Union']schools.append('City College')
print(schools)
How a list works, and order of operation:
schools = ['Columbia University', 'NYU', 'Cooper Union']for school in schools:
print(school) + ' is a school in New York'
Getting to order of operation:
schools = ['Columbia University', 'NYU', 'Cooper Union']for school in schools:
# print(school) + ' is a school in New York'
schoolSentence = school + ' is a school in New York'
schoolSentences = []
schoolSentences.append(schoolSentence)
print(schoolSentences)
Why didn’t it work?
schools = ['Columbia University', 'NYU', 'Cooper Union']# print(schools[2])schoolSentences = []for school in schools:
# print(school) + ' is a school in New York'
schoolSentence = school + ' is a school in New York'
schoolSentences.append(schoolSentence)
print(schoolSentences)
Dictionary Keys:
schools = {'institution name':'Columbia University', 'student poplation':34000, 'degree types':['undergrad','grad']}print(schools)
print(schools['institution name'])
# print(schools[0])
Instagram JSON from a User
Where can we find JSON?
Type the follow url:
and replace the user “columbiagsapp” with a user of your choice. Maybe ‘highlinenyc’?
Type command or ctrl s to save it as a JSON file.
Lets look at this in a more structured way:
Lets open it in Processing:
- To include the data in a new sketch file by selecting in the menu bar, Sketch → Add File. The file should be in the
data
folder of your sketch folder.:
import jsonwith open('columbiagsapp.json') as file:
data = json.load(file)print(data["items"])
lets create a variable to reference the items in our “data” dictionary
items = data["items"]
Now lets find a thumbnail:
thumbnailUrl = data["items"][0]['images']['thumbnail']['url']
Now lets load the thumbnail and draw it on the canvas using the thumbnail url:
size(150, 150)
thumbnail = loadImage(thumbnailUrl)
image(thumbnail, 0, 0)
How do we draw all of the images?
Can we loop over all of the images?
import jsonwith open('columbiagsapp.json') as file:
data = json.load(file)items = data["items"]#put this into a loop
for item in items:
thumbnailUrl = item["images"]["thumbnail"]["url"]
print thumbnailUrlsize(150, 150)
thumbnail = loadImage(thumbnailUrl)
image(thumbnail, 0, 0)
the code above looped over every item and printed the thumbnailUrl string, so when we load the image, we only get the last image we loaded in the for loop:
import jsonwith open('columbiagsapp.json') as file:
data = json.load(file)items = data["items"]xPos = 0
yPos = 0for item in items:
thumbnailUrl = item["images"]["thumbnail"]["url"]
print(thumbnailUrl) xPos += 150
thumbnail = loadImage(thumbnailUrl)
image(thumbnail, xPos, yPos)size(1500,150)
Usually we would fix this by drawing the first thumbnail before the loop.