-2

Say I have a list

list = ["1","2","1","3"]

and I want to get rid of the duplicate 1's but I want to keep one copy so the list would read

list = ["1","2","3"]

I know I can use set() to get rid of the duplicates, but I will not be able to keep a copy. How would I go about keeping a copy?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    What do you mean by keeping a copy? If you make a set out of that list, the list will stay intact. – Francisco Oct 18 '16 at 20:02
  • The answer you've shown is exactly what you would get from converting your list to a set, ignoring issues of ordering... – Sohier Dane Oct 18 '16 at 20:03
  • @Francisco Couzo oh... woops i'm sorry, I haven't actually wrote the code yet and when reading about set() I was under the assumption it removed all the duplicates, and didn't keep any of them. So simply using set() would work? – Matthew Pearson Oct 18 '16 at 20:11

1 Answers1

2

use set and then convert it to list again

lists = ["1","2","1","3"]
new_list = list(set(lists))
Wasi
  • 1,473
  • 3
  • 16
  • 32