I am new to python. I have an error w.r.t list inside a list. So, I have a list inside where the values inside the list are already assigned to a variable. So whatever I insert in those list inside the list it will get assigned to those variable. Like show below
car = list bike = list cycle = list list[[1,2],[2,3],[4,5]]
Now, suppose I won’t give a value for the 3rd list(like shown below). Then I will get an error.
car = list bike = list cycle = list list[[1,2],[2,3]] error message - List index out of range
So I wrote a condition which should ignore it. But I am getting a error. How to ignore if the values is not given?
if list == : continue else: car = list if list == : bike = list if list == : cycle = list SyntaxError: 'continue' not properly in loop
Where am I going wrong? how to give an if condition if there is no list in it? Did I give it correctly?
If I understood your problem correctly, what you’re looking for is a try/except statement. This allows you to do as follows:
try: car = list bike = list bike = list except IndexError: pass
This code allows you to execute the block “try” until there’s an exception, and if an error is catched you just continue the loop. For example you could put something like this inside a for loop as follows:
for list in my_list_of_lists: try: car = list bike = list bike = list except IndexError: continue
finally, regarding this
SyntaxError: ‘continue’ not properly in loop
if you use the break/continue/pass statements they should always be inside a for or while loop, as I’ve shown above.