I have 2 lists. List A and List B. The values from List B are also in List A, but the values from List A are not all in List B. So List A is longer.
I want to check if a value from List A is in List B and if not, insert it at the index of List A.
So after the loop i want to have 2 identical lists.
I know this alone doesnt make much sense, but I also want to do different things in this loop I havent included here for simplification.
lista = ["apple", "banana", "pinapple", "kiwi", "orange"]
listb = ["apple", "pinapple", "kiwi"]
for i in range(len(lista)):
if(lista[i] != listb[i]):
listb.insert(i, lista[i])
print(listb)
When i run this, i get the Error:
Traceback (most recent call last):
File "test.py", line 5, in <module>
if(lista[i] != listb[i]):
IndexError: list index out of range
Why am I being an idiot here? I googled for the last 2 hours and am going mad.
ibecomes 4, yourlistbis['apple', 'banana', 'pinapple', 'kiwi']. Solistb[4]doesn't exist.lista[i]or is itlistb[i]?) then print out what that list contents are and what the value ofiis at the time it fails, and use this information to work out which of your original assumptions about that list is incorrect.