in Education by
I am having two lists list1 = ['Apple', 'Orange', 'Grapes', 'Banana'] list2 = ['Apple', 'Orange'] I want to generate the list3 of those items which are not present in the list2 from list1. For an ex I have to get: list3 = ['Grapes', 'Banana'] Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
There are various ways of doing this some are as follows: 1. In [5]: list(set(list1) - set(list2)) Out[5]: ['Apple', 'Orange'] 2. def diff(list1,list2): x = set(list1).union(set(list2)) y = set(list1).intersection(set(list2)) return list(x - y) 3. list3 = [item for item in list1 if item not in list2] Note: If you want to get difference between two lists containing numbers then: In [5]: set([1, 3]) - set([4, 3]) Out[5]: set([1]) But it is expected to print set([1,4]) ,so for that you will have to use : def diff(list1, list2): return list(set(list1).symmetric_difference(set(list2)))

Related questions

0 votes
    What's the difference? What are the advantages/disadvantages of tuples/lists? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    >>> a=[1,2,3] >>> a.remove(2) >>> a [1, 3] >>> a=[1,2,3] >>> del a[1] >>> a ... above three methods to remove an element from a list? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I have two lists in Python, like these: temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One' ... ways without cycles and checking? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way ... 42, 'food' : 'spam'} Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    E.x. listA = [9,10,11] listB = [12,13,14] Expected outcome: >>> concatenated list [9,10,11,12,13,14] Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    What are lists and tuples? What is the key difference between the two?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Is there any shortcut to make a simple list out of a complex one in python? I already performed for a loop but ... help me with it. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    In the MNIST beginner tutorial, there is the statement accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) tf ... (x,1)? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    What is the difference between back-propagation and feed-forward neural networks? By googling and reading, I found ... feed-forward? Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    In this video from Sebastian Thrun, he says that supervised learning works with "labeled" data and unsupervised ... basic difference. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    It appears that "if x" is almost like short-hand for the longer "if x is not None" syntax. Are they ... would be great to know. Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    In tf.nn.max_pool of tensorflow what is the difference between 'SAME' and 'VALID'? I read in here that ... pool means in tensorflow? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Can someone please tell me the difference between _str_ and _repr_ in python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...