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]
#complete the function's body to return the unique list of numbers
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
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 -
With this way the order of the list is preserved.
If anybody have still doubt you can message me anytime.
I tried that, and it works, I was wondering if there's a way without an additional list.
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.
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.