Home > OS >  Print out dictionaries with list with a condition
Print out dictionaries with list with a condition

Time:10-14

I have a a list with dictionaries inside. If the net followers are > 0 (Followers - following) I want to print out that dictionary.

[{'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']}]

So far i have got this code below. but its only printing firs_names of dictionaries. i want both dictioanries appending to the empty list. any suggestions plz?

empty_list = []
for ducks in duck_collection:
    current_trend = ducks['followers']-ducks['following']
    if current_trend > 0:
        names = ducks['first_name']
        empty_list.append(names)
print(empty_list) 

current output:

['Davey', 'Celest']

desired output:

[{'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': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]

CodePudding user response:

list comprehension with your condition

# d = [{'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']}]
[data for data in d if (data['followers'] - data['following'] >0)]

[{'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': 'Celest',
  'last_name': '',
  'location': 'Throne Room',
  'insane': True,
  'followers': 40189,
  'following': 1,
  'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]

CodePudding user response:

Looks like you're appending the first name instead of the dictionary. Try replacing empty_list.append(names) with empty_list.append(ducks)

CodePudding user response:

duck_collection = [{'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']}]

empty_list = []
for ducks in duck_collection:
    current_trend = ducks['followers']-ducks['following']
    if current_trend > 0:
        names = ducks['first_name']
        empty_list.append(ducks)
print(empty_list)
  • Related