+ 1

Python List Help

I need help understanding list functions. I know that this list = ['AABBCCDDAABB'] new = [] for i in list: if i not in list: new.append(i) print(new) will return ['A', 'B', 'C', 'D'] So how can I return ['A', 'B', 'C', 'D', 'A', 'B']? Basically I need to remove duplicates while retaining the unique order.

27th Mar 2017, 3:09 AM
Aaron Nelson
Aaron Nelson - avatar
7 Answers
27th Mar 2017, 6:42 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 7
I think you put it as a list needlessly. It is basically a string to be processed, so there is no point in storing it as a one-element list. Store it as a string and then the iteration is easy. Unless, of course, you really need to store it as a one-element list precisely.
27th Mar 2017, 2:50 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 7
Plus, my code works for any such case, where you have this one-element list with a string inside and the string can be of different length, the letters can be du-, tri- or multiplicated, no diff.
27th Mar 2017, 2:53 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 6
Cool man. Try experimenting a bit and lose the list's brackets, using purely the string itself. In my code there this allows you to remove all lista[0] references which makes the code cleaner.
27th Mar 2017, 3:11 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 1
awesome Kuba! Thanks!
27th Mar 2017, 7:45 AM
Aaron Nelson
Aaron Nelson - avatar
0
ok full disclosure lol: so I also joined a site http://codewars.com to train my skills more. This is a test case and I've tried many methods already and I also return an error while implementing your solution, Kuba, even tho it works on code playground and IDLE. It was still farther along than what I've tried so far tho. The problem I think lies in that the list is iterable and there are more than one list entries in the test examples. So returning a new list based off of ['AAAABBBCCDAABB'] will error if any other value is given. this is confusing me
27th Mar 2017, 2:42 PM
Aaron Nelson
Aaron Nelson - avatar
0
no it doesn't specifically need to be a list *I don't think*, it just seemed like that was the way to do it based off of my skill set. Maybe I am not understanding the test instructions fully. I'll re-read it and double check.
27th Mar 2017, 3:03 PM
Aaron Nelson
Aaron Nelson - avatar