0
In Python can I assign a Loop to a Variable??
I want to eliminate duplicates from a list with a Bucle for, I am an amateur, just started with this a week ago and I made this code but I believe I can make it shorter using the Bucle for https://code.sololearn.com/caW7w9T2hNom/?ref=app
4 ответов
+ 2
Agustin Peña you don't need to assign your loop to a variable, in your case removing duplicates would be as simple as
ages = set(ages)
On converting the list to a set (a Python collection), it automatically removes duplicates and sorts it too.
https://code.sololearn.com/cpxWoJ7uU4OL/?ref=app
+ 4
You can simply do this
a=[]
for i in ages:
if i not in a:
a.append(i)
a.sort()
print(a)
or this way
b=[]
list(b.append(i) for i in ages if i not in b)
b.sort()
print(b)
but you can't assign a loop to a variable
+ 2
ages = [15, 15, 15, 16, 17, 17, 16, 18, 18, 19, 20, 20, 20, 15]
mylist = list(set(ages))
print(mylist)
what is a "Bucle for"? Googled it and got nothing.
+ 1
Woow thanks a lot! You guys really helped me a lot!! 🙌🙌