2
list1=['f','l','a','m','e','s'] #This is the predefined list
list2=['e','e','f','a','s','a'] #This is the list with repitition
x=list(set(list2)) # I want to remove duplicates
print(x)

Here I want the variable x to retain the order which list1 has. For example, if at one instance set(list2) produces the output as ['e','f','a','s'], I want it to produce ['f','a','e','s'] (Just by following the order of list1). Can anyone help me with this?

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40

4 Answers4

1

Construct a dictionary that maps characters to their position in list1. Use its get method as the sort-key.

>>> dict1 = dict(zip(list1, range(len(list1))))
>>> sorted(set(list2), key=dict1.get)
['f', 'a', 'e', 's']
timgeb
  • 76,762
  • 20
  • 123
  • 145
0

This is one way using dictionary:

list1=['f','l','a','m','e','s'] #This is the predefined list
list2=['e','e','f','a','s','a'] #This is the list with repitition
x=list(set(list2)) # I want to remove duplicates

d = {key:value for value, key in enumerate(list1)}
x.sort(key=d.get)
print(x)
# ['f', 'a', 'e', 's']
Austin
  • 25,759
  • 4
  • 25
  • 48
0

Method index from the list class can do the job:

sorted(set(list2), key=list1.index)
Valentin B.
  • 602
  • 6
  • 18
  • 1
    This is pretty concise but has sub-optimal complexity. `index` is scanning `list1` for every character in `set(list2)`. – timgeb Apr 27 '18 at 12:24
  • In my book more concise/readable = more pythonic. This is vastly subjective though :) – Valentin B. Apr 27 '18 at 12:27
0

What is best usually depends on actual use. With this problem it is important to know the expected sizes of the lists to choose the most efficient approach. If we are keeping much of the dictionary the following query works well and has the additional benefit that it is easy to read.

set2 = set(list2)
x = [i for i in list1 if i in set2]

It would also work without turning list2 into a set first. However, this would run much slower with a large list2.

Hinni
  • 166
  • 5