In: Computer Science
PHTHON 3
Write code that generates a list of the names of all the senders of the messages in fb_data. Store it in a variable called senders.
fb_data = {
"data": [
{
"id": "2253324325325123432madeup",
"from": {
"id": "23243152523425madeup",
"name": "Jane Smith"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "Understand. Extract. Repeat.",
"type": "status",
"created_time": "2014-10-03T02:07:19+0000",
"updated_time": "2014-10-03T02:07:19+0000"
},
{
"id": "2359739457974250975madeup",
"from": {
"id": "4363684063madeup",
"name": "John Smythe"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "Here is a fun link about programming",
"type": "status",
"created_time": "2014-10-02T20:12:28+0000",
"updated_time": "2014-10-02T20:12:28+0000"
}]
}
Solution:
Look at the code and comments for better understanding.................
Screenshot of the code:
Output:
Code to copy:
fb_data = {
"data": [
{
"id": "2253324325325123432madeup",
"from": {
"id": "23243152523425madeup",
"name": "Jane Smith"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "Understand. Extract. Repeat.",
"type": "status",
"created_time": "2014-10-03T02:07:19+0000",
"updated_time": "2014-10-03T02:07:19+0000"
},
{
"id": "2359739457974250975madeup",
"from": {
"id": "4363684063madeup",
"name": "John Smythe"
},
"to": {
"data": [
{
"name": "Your Facebook Group",
"id": "432542543635453245madeup"
}
]
},
"message": "Here is a fun link about programming",
"type": "status",
"created_time": "2014-10-02T20:12:28+0000",
"updated_time": "2014-10-02T20:12:28+0000"
}
]
}
#senders ia list variable that stores all the names of the senders
senders = []
#iterate over every detail of the data list
for detail in fb_data["data"]:
#get the name of the sender from the dictionary having the key "from"
name = detail["from"]["name"]
senders.append(name)
print("Senders Names :")
print(senders)
I hope this would help.............................:-))