0

I have a list of years:

years = ["2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013"]

And a list of info I want to get from web scraping:

info = ["played_games", "games_won", "efectivity", "championship_won", "finals", "semi-finals", "quarterfinals"]

I need to iterate though years so I can make empty lists with the names of the elements in info + the correspondent year, so I can append the info later, i.e.:

played_games2020 = []
games_won2020 = []
efectivity2020 = []
etc
played_games2019 = []
games_won2019 = []
efectivity2019 = []
etc

Any help would be much appreciated! Regards

dubbbdan
  • 2,650
  • 1
  • 25
  • 43
  • 4
    Maybe a better way to organize this would be `d = collections.defaultdict(list)` ? And then you can `d['played_games'].append(..)` etc – han solo Aug 23 '20 at 15:56
  • Does this answer your question? [Creating empty lists with the name of the elements of another list](https://stackoverflow.com/questions/42771692/creating-empty-lists-with-the-name-of-the-elements-of-another-list) –  Aug 23 '20 at 16:05
  • https://stackoverflow.com/a/18098373/9586997 This answer will help you. Write the logic yourself. – aryanveer Aug 23 '20 at 16:10
  • or `d = collections.defaultdict(lambda: defaultdict(list))` and then `d[year][detail].append(..)` – han solo Aug 23 '20 at 16:12

4 Answers4

1

EDIT:

I believe a faster solution is to use dict comprehension with itertools.product. The solution would be simply a line of code:

import itertools
example_dict = {x:[] for x in [x[0]+x[1] for x in itertools.product(info,years)]}

The output is the same as the following solution.

As han solo suggested in their comment, this seems like a better fit defaultdict(list). Essentially the solution would be:

from collections import defaultdict
example_dict = defaultdict(list)
for i in years:
   for j in info:
       example_dict[j+i]
print(example_dict)

Outputs:

defaultdict(list,
            {'championship_won2013': [],
             'championship_won2014': [],
             'championship_won2015': [],
             ...
             'semi-finals2018': [],
             'semi-finals2019': [],
             'semi-finals2020': []})
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
0

Using 2 loops , you can try this :

years = ["2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013"]

info = ["played_games", "games_won", "efectivity", "championship_won", "finals", "semi-finals", "quarterfinals"]

all_arrays_dict = {}
for x in years:
  for y in info:
    all_arrays_dict[y+x] = []

print(all_arrays_dict)

And this is the output dictionary :

{
  "played_games2020": [],
  "games_won2020": [],
  "efectivity2020": [],
  "championship_won2020": [],
  "finals2020": [],
  "semi-finals2020": [],
  "quarterfinals2020": [],
  "played_games2019": [],
  "games_won2019": [],
  .
  .
  .
}
Sowjanya R Bhat
  • 1,128
  • 10
  • 19
0

you can create a list that contains n empty lists. with n = len(info) * len(years). where the list at pos 0 is (info[0],years[0]) , list at 1 is (info[0],years[1])....

if you want, you can also store the names in a list of string, with same philosophy (double loop on info and years).

ibra
  • 1,164
  • 1
  • 11
  • 26
0

You may use a one-dimension dict and merge in one string the itme and the year that isn't nice, you'd rather use a 2-dimension dictionnary and fill up with empty list at first, then access throught the both infos : item + date

  • an item then a year

    values = {i: {year: [] for year in years} for i in info}
    
    {'played_games': {'2020': [], '2019': [], '2018': [], '2017': [], '2016': [], '2015': [], '2014': [], '2013': []}, 
     'games_won':    {'2020': [], '2019': [], '2018': [], '2017': [], '2016': [], '2015': [], '2014': [], '2013': []}, 
     'efectivity':   {'2020': [], '2019': [], '2018': [], '2017': [], '2016': [], '2015': [], '2014': [], '2013': []}, 
      ...}
    
    # add data like
    values["played_games"]['2014'].append("foo")
    
  • a year then an item

    values = {year: {i: [] for i in info} for year in years}
    
    {'2020': {'played_games': [], 'games_won': [], 'efectivity': [], 'championship_won': [], 'finals': [], 'semi-finals': [], 'quarterfinals': []}, 
     '2019': {'played_games': [], 'games_won': [], 'efectivity': [], 'championship_won': [], 'finals': [], 'semi-finals': [], 'quarterfinals': []}, 
     '2018': {'played_games': [], 'games_won': [], 'efectivity': [], 'championship_won': [], 'finals': [], 'semi-finals': [], 'quarterfinals': []}, 
      ...}
    
    # add data like
    values['2014']["played_games"].append("foo")
    
azro
  • 53,056
  • 7
  • 34
  • 70