duck_1 = {
"first_name": "Davey",
"followers": 12865,
"following": 120,
"weapons": ["wit", "steely stare", "devilish good looks"]
}
duck_2 = {
"first_name": "Jim",
"followers": 123,
"following": 5000,
"weapons": ["squeak"]
}
duck_3 = {
"first_name": "Celest",
"followers": 40189,
"following": 1,
"weapons": ["politics", "dance moves", "chess grandmaster", "immortality"]
}
duck_collection = [ duck_1, duck_2, duck_3 ]
for duck in duck_collection:
duck1=(duck_1['followers']-duck_1['following'])
print(duck1)
duck2=(duck_2['followers']-duck_2['following'])
print(duck2)
duck3=(duck_3['followers']-duck_3['following'])
the output is:
12745
-4877
40188
12745
-4877
40188
12745
-4877
40188
i want the output to be:
12745
-4877
40188
and also result should be append to Trendy Ducks: [12745, -4877, 40188]
CodePudding user response:
You can use a list comprehension.
trendyDucks = [duck['followers'] - duck['following'] for duck in duck_collection]
print(trendyDucks)
CodePudding user response:
If you don't know list comprehension you could use just this:
trendyDucks = []
for duck in duck_collection:
trendyDucks.append(duck['followers']-duck['following'])