0

I want to acsess the values in my list using indexes but I cant since its in a nested list can someone please help where I can acsess the values in my list properly with no errors

def get_info():
    infile = open('lol.txt','r')
    f = infile.readlines()
    id = input('enter your id:')
    new_list = []
    for line in f:
        if line.split(':')[0] == id:
            (line.strip())
            new_list.append(line.split(':'))
    print(new_list)
    print(new_list[1]) # error



get_info()

Output : [['1111', 'zoki', '2', '3', '1\n']]

loli loli
  • 11
  • 1

1 Answers1

0

You can use the extend() function to add each item from the split() into the list.

Like this:

for line in f:
    if line.split(':')[0] == id:
        new_list.extend(line.split(':'))
Nathan Roberts
  • 828
  • 2
  • 10