0

I'm making lists for every line in the text file with the first element being "List[linenumber]", for example, "Line1". However, I need to name these lists so I can use each one later on in the program, so I need to name each list with the first element (e.g. List1 = ["List1", "string", int])

    lines = 0
    user = input ("Enter GTIN-8 Code For Desired Product: ")
    with open('Task2.csv') as File:
        reader = csv.reader(File, delimiter=',', quotechar=',',quoting=csv.QUOTE_MINIMAL)
        for row in reader:
            lines = int(lines)
            lines += 1
            lines = str(lines)
            lists = "List"
            lists = lists + lines
            lists = [lists] + row
            print (lists)

How can I do this? Is there a line of code I can add to the for loop?

Thanks for helping.

1 Answers1

1

Do you really have to do it this way and saving every list individually in a new variable?

In my opinion a better option would be to use a 2-dimensional List and accessing your elements via their indices, e.g. lines[0][1]. This would be a "list of lists", where the outer list contains your lines, each line in the format of another list.

You can read more about lists of lists here.

lobetdenherrn
  • 303
  • 1
  • 8