-2
subs = ['Py', 'Jupyter', 'Spy', 'VisualStudio']
test_list = ['Python', 'Jupyter Notebook', 'Spyder',] 
set1=set(subs)
set2=set(test_list)
print(list(set2 - set1))

Actual Output
['Python', 'Spyder', 'Jupyter Notebook']

Expected Output
['Jupyter Notebook', 'Python', 'Spyder','VisualStudio']

Solution

test_list=['Python', 'Jupyter Notebook', 'Spyder',]
subs=['Py', 'Jupyter', 'Spy', 'VisualStudio']
set_sub=set(subs)
tem=[]
for elem in subs:
    for n in test_list:
        if elem not in n:
            continue
        tem.append(elem)
print(list(set(tem)),'\n\n')
set_tem=set(tem)
print(set_sub - set_tem )
list_diff=list(set_sub - set_tem)
for s in list_diff:
    test_list.append(s)
print(test_list)

This is the solution I was expecting. Thank you everyone for helping me.

  • 2
    Can you post some more information? – rak3sh Mar 02 '20 at 07:01
  • This seems like a question related to competitive programming, even if it's not, try to include more details which makes it more easier for everyone to understand the question. – Saurav Saha Mar 02 '20 at 07:19

2 Answers2

2

When you took difference, that is list(set2 - set1), it will result in those elements in your set2 which are not in set1 If you want to explore more about list difference you can checkout this existing stackover question.

Example :

s1 = [1, 2, 3, 4]
s2 = [2, 3, 5]
print(set(s1) - set(s2)) ==> {1, 4}

What you can do for your query is something like this :

tmp_set = set(set1 + set2) ==> This will contains unique elements from both set1 and set2
print([*tmp_set, ])

Your final code will look like this:

set1 = ['Py', 'Jupyter', 'Spy', 'VisualStudio']
set2 = ['Python', 'Jupyter Notebook', 'Spyder',] 
tmp_set = set(set1 + set2)
print([*tmp_set, ])
Rahul Goel
  • 842
  • 1
  • 8
  • 14
0

The operation which matches the title symmetric_difference rather than difference (which is what set - set does): difference gives you all the elements of the first set which are not in the second, symmetric_difference gives you all the elements which are in either set but not both.

The operator for symmetric_difference is ^.

However that would not yield your "expected output": your two lists don't share any element, so the symmetric difference will be all items as it works by equality.

If you want something more complicated (e.g. all elements which are not prefixes of an other) you will have to hand-roll it using a loop or a set comprehension.

Masklinn
  • 34,759
  • 3
  • 38
  • 57