Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)

In python, I have two lists:

temp1 = ['One', 'Two', 'Three', 'Four']

temp2 = ['One', 'Two']

I just want to create a third list that has the values of the first list items which are not there in the second list. For example

temp3 = ['Three', 'Four']

Is there any method of doing this?

1 Answer

0 votes
by (26.4k points)

Try below code:

In [5]: list(set(temp1) - set(temp2))

Out[5]: ['Four', 'Three']

Be careful that:

In [5]: set([1, 2]) - set([2, 3])

Out[5]: set([1]) 

where you may expect/need it to rise to set([1, 3]). On the off chance that you do need set([1, 3]) as your answer, you'll need to utilize set([1, 2]).symmetric_difference(set([2, 3])).

Are you interested to learn the concepts of python? Join the python training course fast!

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Oct 3, 2019 in Python by Tech4ever (20.3k points)

Browse Categories