How can i get unique list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
12th Jan 2020, 2:13 PM
Yessine Agrebi
Yessine Agrebi - avatar
19 Answers
+ 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))
12th Jan 2020, 2:18 PM
Paul
Paul - avatar
+ 4
You can transform the list into dictionary keys and back into a list. https://code.sololearn.com/c6otnBb5ELXS/?ref=app
14th Jan 2020, 2:47 AM
George Alves
George Alves - avatar
+ 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.
12th Jan 2020, 2:45 PM
Paul
Paul - avatar
+ 2
HonFu here is a generator solution for unique elements https://code.sololearn.com/cZ1BL2zAPXj2/?ref=app
12th Jan 2020, 4:58 PM
Tibor Santa
Tibor Santa - avatar
+ 2
https://code.sololearn.com/c4cIWEspujsd/?ref=app
14th Jan 2020, 11:28 AM
J Santhosh Kumar
J Santhosh Kumar - avatar
+ 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.
14th Jan 2020, 12:03 PM
Elliot
Elliot - avatar
+ 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)
12th Jan 2020, 2:28 PM
Ipang
+ 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.
12th Jan 2020, 2:34 PM
HonFu
HonFu - avatar
+ 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.
12th Jan 2020, 3:21 PM
Ipang
+ 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.
12th Jan 2020, 3:31 PM
HonFu
HonFu - avatar
+ 1
Ipang problem solved ! Thank you
12th Jan 2020, 3:39 PM
Yessine Agrebi
Yessine Agrebi - avatar
+ 1
Tibor Santa, ha, nice! 😀
12th Jan 2020, 5:24 PM
HonFu
HonFu - avatar
+ 1
Step 1 - Convert List into Set (It remove the duplicate value ). Step 2 - Convert Set into list .
13th Jan 2020, 5:46 PM
Aacharan Jain
Aacharan Jain - avatar
0
What's wrong in this guys? text = str(input()) def reverse(text): return text[::-1] print("the reversed text is: "+reverse(text))
12th Jan 2020, 2:30 PM
Yessine Agrebi
Yessine Agrebi - avatar
0
Yessine Agrebi Just replace the + with a comma when printing the result print("the reverse text is:", reverse(text))
12th Jan 2020, 3:24 PM
Ipang
0
Ipang This is the error : StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
12th Jan 2020, 3:33 PM
Yessine Agrebi
Yessine Agrebi - avatar
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.
12th Jan 2020, 3:38 PM
Ipang
0
use set() method
13th Jan 2020, 12:50 AM
‎محمد عبدالكريم ابوخشيم‎
‎محمد عبدالكريم ابوخشيم‎ - avatar
- 1
2²we or
13th Jan 2020, 2:43 PM
Алексадр Шведов