How can i get unique list? | SoloLearn: Learn to code for FREE!

+3

# How can i get unique list?

https://code.sololearn.com/cnc5HJk9KngK/?ref=app

1/12/2020 2:13:04 PM

Yessine Agrebi

+5

Like this convert to set and back to list: no_list = [22,22,2,1,11,11,2,2,3,3,3,4,5,5,5,55,55,66] def unique_list(l): #complete the function's body to return the unique list of numbers return list(set(l)) print(unique_list(no_list))

+4

You can transform the list into dictionary keys and back into a list. https://code.sololearn.com/c6otnBb5ELXS/?ref=app

+3

Ipang set is defined as unordered and unique. It doesn't care about the order of the input. But you can always sort the result.

+2

HonFu here is a generator solution for unique elements https://code.sololearn.com/cZ1BL2zAPXj2/?ref=app

+2

https://code.sololearn.com/c4cIWEspujsd/?ref=app

+2

I am using Paul's list to explain the difference list1 = [22,22,2,1,11,11,2,2,3,3,3,4,5,5,5,55,55,66] #We can get unique lists by two ways 1.By using Sets def func1(l): return list(set(l)) print(func1(list1)) But here our result is going to be unordered, because sets are unordered. So to preserve the order of the list we can use 2nd way. 2.With Python 3.7 Insertion and deletion in dictionaries is ordered. So we can use 'fromkeys' method to create a unique list as following - def func2(l): return list({}.fromkeys(l)) print(func2(list1)) With this way the order of the list is preserved. If anybody have still doubt you can message me anytime.

+1

Paul Jacobs How can we preserve the order for the resulting list? as I tried this code it appears the order for the numbers changed (sequentially)

+1

If you want to maintain the order, loop over list a and append an element to list b if it's not 'in' there yet.

+1

HonFu Thank you. I tried that, and it works, I was wondering if there's a way without an additional list. Paul Jacobs Thank you. Yes I understand that much, I just thought that the set will add the numbers sequentialy, as they appear in the source list, ignoring duplicates of course.

+1

Ipang, you could also go by generator function, yielding every value separately if it's not 'in' the original list up to index. Or you loop and print out each item if it's not in the part of the list up to that index, but if you use slices for that, you'll end up copying even more. You can manually inner-loop until index and check by == if it was there... It seems all cumbersome. I think it's natural to create a subsum list, or tuple, or if you strictly want no copy, generator. In short: If you don't create a 'sequence', you can't rely well on the order. EDIT: Not even sure anymore that I'd know a way to create a generator for that.

+1

Ipang problem solved ! Thank you

+1

Tibor Santa, ha, nice! 😀

+1

Step 1 - Convert List into Set (It remove the duplicate value ). Step 2 - Convert Set into list .

0

What's wrong in this guys? text = str(input()) def reverse(text): return text[::-1] print("the reversed text is: "+reverse(text))

0

Yessine Agrebi Just replace the + with a comma when printing the result print("the reverse text is:", reverse(text))

0

Ipang This is the error : StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

0

Yessine Agrebi `raw_input` is for Python 2, `input` is for Python 3. I don't know what the "frontend" word there even mean.

0

use set() method

-1

2²we or