vendredi 25 septembre 2020

Python while loop out of range: ways to solve this?

I have the following code, and I cannot change the len of none of these lists:

first_list = [1, 2, 3, 4, 5, 6, 7, 8]
second_list = [1, 2]
third_list = [55, 99]

i = 0

while i < len(first_list):
    if first_list[i] == second_list[i]:
        first_list[i] = third_list[i]
    else:
        # do something
    i = i + 1

I also have tried using for loop like this:

for f in first_list:
    for s in second_list:
        for t in third_list:
            # do something similar to the while loop

But I only got the last value and couldn't use the rest of them.

I can understand that first_list is bigger than second_list and third_list, and that's why i'm getting this error. But since I cannot change the length of these lists, how can I change my code to do what I need to do? (the if condition).

Thank you very much.

Aucun commentaire:

Enregistrer un commentaire