1

Bit of a simple question. I would like to iterate through 2 lists, the first to name the variable and the second to read a csv. I want to assign the csv to the variable based on the name my list is iterating through.

Here is a sample of the code I have written:

lists = ['one','two','three','four','five','six','seven','eight']
for i in lists:
    for j in range(1,9):
        i = merger(path,path_2,j)

In this case I am not assigning the items in lists to the variable i (as the variable name)

The function merge merely takes two paths, reads the csvs from each and merges them using pd.concat

Emm
  • 2,367
  • 3
  • 24
  • 50
  • Not really possible in `python`. Use a dictionary like: `csv_dict = {}` and then in the loop: `csv_dict[i] = merger(path,path_2,j)` – Aryerez Nov 11 '19 at 12:06

1 Answers1

0

This question has been answered here. However, exec isn't always very efficient.

You should consider using a dictionary with the names as keys instead.

lists = ['one','two','three','four','five','six','seven','eight']
csv = {}
for i in lists:
    for j in range(1,9):
        csv[i] = merger(path, path_2, j)
Donshel
  • 563
  • 4
  • 13