Search code examples
pythonlist

How to fill a list by the rest of the values?


I have a full list: f = [A,B,C,D,E].

And short list: s = [A,B,D].

I want to get resulting list as short list + all the rest of the values from the full list that there are not in the short list.

Result should be:

nlist = [A,B,D,C,E]

I tried two loops:

nlist = s
for f_item in f:
  for s_item in s:
     if s_item not in f:
         nlist.append(f_item)

But I think it is wrong.


Solution

  • Use list comprehension:

    lst = short_lst + [x for x in long_lst if x not in short_lst]