-1

New to Python here and just looking for a little help. I have a list of dictionaries from a json file as follows:

x = [{'competition_id': 16, 'season_id': 4, 'country_name': 'Europe', 'competition_name': 'Champions League', 'competition_gender': 'male', 'season_name': '2018/2019'},
     {'competition_id': 16, 'season_id': 1, 'country_name': 'Europe', 'competition_name': 'Champions League', 'competition_gender': 'male', 'season_name': '2017/2018'},
     {'competition_id': 43, 'season_id': 3, 'country_name': 'International', 'competition_name': 'FIFA World Cup', 'competition_gender': 'male', 'season_name': '2018'}
     ]

Is there a way that I could filter this to just show me the "competition_name" and corresponding "season_name" ?

I've tried the following:

newList = [i["competition_name"]for i in x],[i["season_name"]for i in x]

Which provides the following output as a tuple:

(['Champions League', 'Champions League', 'FIFA World Cup'], ['2018/2019', '2017/2018', '2018'])

But for a long list it's still hard to read, is there a way to print the output so the key/value pairs for each item are next to each other, eg

(['Champions League', '2018/2019'],['Champions League','2017/2018'], ['FIFA World Cup','2018'])

Hope that makes sense, thanks

Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
J.Raze
  • 1
  • Does this answer your question? [Getting a list of values from a list of dicts](https://stackoverflow.com/questions/7271482/getting-a-list-of-values-from-a-list-of-dicts) – sahasrara62 Jun 05 '21 at 09:35

5 Answers5

0

Try

for i in x:
   newList.append([i.get('competition_name'), i.get('season_name')])
Nadia Chibrikova
  • 4,916
  • 1
  • 15
  • 17
0

You were going in the correct direction but then made mistake while creating output list.

Try this:

newList = [[i['competition_name'], i['season_name']] for i in x]

Output :

[['Champions League', '2018/2019'], ['Champions League', '2017/2018'], ['FIFA World Cup', '2018']]
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
  • Thanks, I tried so many combos of this and could not get it to work so I obviously did not try this one!! Thanks again – J.Raze Jun 05 '21 at 09:21
0

If you already have the two lists, you can also use Python's zip() function:

competition_names = ['Champions League', 'Champions League', 'FIFA World Cup']
season_names = ['2018/2019', '2017/2018', '2018']

competitions = list(zip(competition_names, season_names))

print(competitions)

This prints:

[('Champions League', '2018/2019'), ('Champions League', '2017/2018'), ('FIFA World Cup', '2018')]
Remco646
  • 132
  • 2
  • 7
0

You can do that in more steps:

list_competition = [i['competition_name'] for i in x]
list_season = [i['season_name'] for i in x]
new_list=[]
for i in range(len(list_competition)):
    new_list.append((list_competition[i],list_season[i]))

this will be your output:

[('Champions League', '2018/2019'), ('Champions League', '2017/2018'), ('FIFA World Cup', '2018')]
Nicolas Tabet
  • 139
  • 1
  • 5
0

with lambda you can achive this as

x = [{'competition_id': 16, 'season_id': 4, 'country_name': 'Europe', 'competition_name': 'Champions League', 'competition_gender': 'male', 'season_name': '2018/2019'},
     {'competition_id': 16, 'season_id': 1, 'country_name': 'Europe', 'competition_name': 'Champions League', 'competition_gender': 'male', 'season_name': '2017/2018'},
     {'competition_id': 43, 'season_id': 3, 'country_name': 'International', 'competition_name': 'FIFA World Cup', 'competition_gender': 'male', 'season_name': '2018'}
     ]

result = list(map(lambda a: [a.get("competition_name"), a.get("season_name")], x))
print(result)

output

[['Champions League', '2018/2019'], ['Champions League', '2017/2018'], ['FIFA World Cup', '2018']]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • Great, I had the method to extract one item with lambda but that was without using .get. This works well. Thanks! – J.Raze Jun 05 '21 at 09:41