Home > Enterprise >  for loop in list index
for loop in list index

Time:03-06

I have a simple yet difficult question for me, I am familiar with for loops in python but as I am trying to loop through a range in the list index it gives me an error.

for x in range(0,9):
    fakelist[f'{x}'].split('\n\n')

This code gives me a TypeError.

CodePudding user response:

What if you use something like this: fakelist[x].split('\n\n') Holpe it helps!

CodePudding user response:

Assuming fakelist is a Python List datatype

Your code is trying to pass a string datatype variable as index to the into the list - fakelist Hence the error.

for x in range(0,9):
    print(type(f'{x}'))
    fakelist[f'{x}'].split('\n\n')    

If you run the above code you can see that the index what you are trying to pass is a string type

Try to use the following instead.

for x in range(0,9):
    fakelist[x].split('\n\n')
  • Related