31

If I have two lists (may be with different len):

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

result = [11,22,33,44]

im doing:

for element in f:
    if element in x:
        f.remove(element)

I'm getting

result = [11,22,33,44,4]
Dharman
  • 30,962
  • 25
  • 85
  • 135
user3683587
  • 339
  • 1
  • 3
  • 7

11 Answers11

60

UPDATE:

Thanks to @Ahito:

In : list(set(x).symmetric_difference(set(f)))

Out: [33, 2, 22, 11, 44]

This article has a neat diagram that explains what the symmetric difference does.

OLD answer:

Using this piece of Python's documentation on sets:

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}

I came up with this piece of code to obtain unique elements from two lists:

(set(x) | set(f)) - (set(x) & set(f))

or slightly modified to return list:

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list

Here:

  1. | operator returns elements in x, f or both
  2. & operator returns elements in both x and f
  3. - operator subtracts the results of & from | and provides us with the elements that are uniquely presented only in one of the lists
Iopheam
  • 1,065
  • 10
  • 11
25

If you want the unique elements from both lists, this should work:

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

res = list(set(x+f))
print(res)
# res = [1, 2, 3, 4, 33, 11, 44, 22]
gdanezis
  • 619
  • 4
  • 7
18

Based on the clarification of this question in a new (closed) question:

If you want all items from the second list that do not appear in the first list you can write:

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

result = set(f) - set(x) # correct elements, but not yet in sorted order
print(sorted(result)) # sort and print

# Output: [11, 22, 33, 44]
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
7
x = [1, 2, 3, 4]

f = [1, 11, 22, 33, 44, 3, 4]

list(set(x) ^ set(f))

[33, 2, 22, 11, 44]
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
  • 6
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Waqar UlHaq Feb 26 '20 at 14:57
  • What does the ^ do? – Adrian Guerra Jan 06 '21 at 15:00
6

if you want to get only unique elements from the two list then you can get it by..

a=[1,2,3,4,5]
b= [2,4,1]
list(set(a) - set(b))
OP:- [3, 5]
Javed
  • 1,613
  • 17
  • 16
6

Input :

x = [1,2,3,4]

f = [1,11,22,33,44,3,4]

Code:

l = list(set(x).symmetric_difference(set(f)))
print(l)

Output :

[2, 22, 33, 11, 44]
Ahito
  • 333
  • 3
  • 8
  • 15
3

Your method won't get the unique element "2". What about:

list(set(x).intersection(f))
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
3

Simplified Version & in support of @iopheam's answer.

Use Set Subtraction.

# original list values
x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

# updated to set's
y = set(x)  # {1, 2, 3, 4}
z = set(f)  # {1, 33, 3, 4, 11, 44, 22}

# parsed to the result variable
result = z - y  # {33, 11, 44, 22}

# printed using the sorted() function to display as requested/stated by the op.
print(f"Result of f - x: {sorted(result)}")
# Result of f - x: [11, 22, 33, 44]
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
0
v_child_value = [{'a':1}, {'b':2}, {'v':22}, {'bb':23}]
shop_by_cat_sub_cats = [{'a':1}, {'b':2}, {'bbb':222}, {'bb':23}]


unique_sub_cats = []
for ind in shop_by_cat_sub_cats:
    if ind not in v_child_value:
        unique_sub_cats.append(ind)

unique_sub_cats = [{'bbb': 222}]

user3683587
  • 339
  • 1
  • 3
  • 7
0

Python code to create a unique list from two lists :

a=[1,1,2,3,5,1,8,13,6,21,34,55,89,1,2,3]
b=[1,2,3,4,5,6,7,8,9,10,11,12,2,3,4]
m=list(dict.fromkeys([a[i] for i in range(0,len(a)) if a [i] in a and a[i] in b and a[i]]))

print(m)
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
0
L=[] 
For i in x:
    If i not in f:
        L. Append(I)
For i in f:
    If I not in x:
        L. Append(I)
Return L
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54