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

29th May 2020, 8:59 PM
Agustin Peña
Agustin Peña - avatar
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
29th May 2020, 9:37 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 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
29th May 2020, 9:13 PM
Abhay
Abhay - avatar
+ 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.
29th May 2020, 9:37 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Woow thanks a lot! You guys really helped me a lot!! 🙌🙌
29th May 2020, 9:47 PM
Agustin Peña
Agustin Peña - avatar