Answer the question
In order to leave comments, you need to log in
What's wrong with the For loop in Python?
Hello!
I'm just starting to learn Python and started parsing lists. Everything seemed to be clear, until one moment.
There is a task:
Make a program that would remove duplicate numbers from the list.
Solution:
I started solving the problem with a For loop, code:
numbers = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5] #список с нарочными дублями
item_2 = 0
for item in numbers:
if item_2 == item:
print(item) #это я так дебажу
numbers.remove(item)
else:
item_2 = item
print(numbers)
numbers = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
# удалил отсюда item_2 = 0
for item in numbers:
item_2 = numbers[item] #добавил эту строку
if item_2 == item:
print(item)
numbers.remove(item)
else:
item_2 = item #item_2 = 2
print(numbers)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question