Home > front end >  How to print out specific content from list with nested dictionaries
How to print out specific content from list with nested dictionaries

Time:10-13

Hi i have got this list with dictionaries

[{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120, 'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None}, {'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123, 'following': 5000, 'weapons': ['squeak'], 'remorse': None}, {'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]

I have got three dictionaries inside this list and I need to print twitter trend of each dictionary i.e twitter followers - following = trend. Also i need to make a en empty list and append these 3 values to that empty list. So far I have got this code below any suggestion plz?

trend = []
followers = int(duck_collection['followers']) - int(duck_collection['following'])
for f in followers:
    print(f)
    trend.append(f)
print(trend)

the output should be:

12745
-4877
40188
Trend: [12745, -4877, 40188]

CodePudding user response:

So what I have understood from your question is that you need to store the difference of followers and following value for each user dictionary and store it in an empty list called trend.

You can do it in a few lines using list comprehension as shown here:

user_stats = [{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120, 'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None}, {'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123, 'following': 5000, 'weapons': ['squeak'], 'remorse': None}, {'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]

trend = [user["followers"] - user["following"] for user in user_stats]

Thus you will get your trend list.

CodePudding user response:

You must iterate over all the items in the list and calculate the trend for each and then append it to the trend list.

trend = []
for item in duck_collection:
    current_trend = item['followers']-item['following']
    print(current_trend)
    trend.append(current_trend)
print(trend)

Alternatively, without printing you can use list comprehension to create the trend list -

trend = [item['followers']-item['following'] for item in duck_collection]

CodePudding user response:

You can use a list comprehension.

l = [{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120, 'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None}, {'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123, 'following': 5000, 'weapons': ['squeak'], 'remorse': None}, {'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]
trend = [x['followers'] - x['following'] for x in l]
print(trend)

CodePudding user response:

Not the most readable answer, but a functional approach would be:

>>> from operator import sub, itemgetter
>>> from itertools import starmap
>>> list(starmap(sub, map(itemgetter('followers', 'following'), duck_collection)))
[12745, -4877, 40188]

You could use just operator.itemgetter and a comprehension though:

[x - y for x, y in map(itemgetter('followers', 'following'), duck_collection)]
  • Related