How do you exclude a list from a list in python?

Possible Duplicate:
Get difference from two lists in Python

What is a simplified way of doing this? I have been trying on my own, and I can't figure it out. list a and list b, the new list should have items that are only in list a. So:

a = apple, carrot, lemon
b = pineapple, apple, tomato
new_list = carrot, lemon

I tried writing code, but every time it always returns the whole list a to me.

asked Jul 11, 2012 at 14:14

0

You can write this using a list comprehension which tells us quite literally which elements need to end up in new_list:

a = ['apple', 'carrot', 'lemon']
b = ['pineapple', 'apple', 'tomato']

# This gives us: new_list = ['carrot' , 'lemon']
new_list = [fruit for fruit in a if fruit not in b]

Or, using a for loop:

new_list = []
for fruit in a:
    if fruit not in b:
        new_list.append[fruit]

As you can see these approaches are quite similar which is why Python also has list comprehensions to easily construct lists.

answered Jul 11, 2012 at 14:18

Simeon VisserSimeon Visser

115k18 gold badges176 silver badges178 bronze badges

1

You can use a set:

# Assume a, b are Python lists

# Create sets of a,b
setA = set[a]
setB = set[b]

# Get new set with elements that are only in a but not in b
onlyInA = setA.difference[b]

UPDATE
As iurisilvio and mgilson pointed out, this approach only works if a and b do not contain duplicates, and if the order of the elements does not matter.

Zulu

8,0799 gold badges46 silver badges55 bronze badges

answered Jul 11, 2012 at 14:22

5

You may want this:

a = ["apple", "carrot", "lemon"]
b = ["pineapple", "apple", "tomato"]

new_list = [x for x in a if [x not in b]]

print new_list

answered Jul 11, 2012 at 14:21

martinmartin

2,43024 silver badges28 bronze badges

Would this work for you?

a = ["apple", "carrot", "lemon"]
b = ["pineapple", "apple", "tomato"]

new_list = []
for v in a:
    if v not in b:
        new_list.append[v]

print new_list

Or, more concisely:

new_list = filter[lambda v: v not in b, a]

answered Jul 11, 2012 at 14:17

Alex WilsonAlex Wilson

6,62026 silver badges44 bronze badges

2

How about using sets [or the built in set since Sets was deprecated in 2.6]?

from sets import Set
a = Set[['apple', 'carrot', 'lemon']]
b = Set[['pineapple','apple','tomato']]
new_set =  a.difference[b]
print new_set

gives the output

Set[['carrot', 'lemon']]

istruble

13k2 gold badges49 silver badges51 bronze badges

answered Jul 11, 2012 at 14:23

StuGreyStuGrey

1,4299 silver badges20 bronze badges

4

How do you exclude an item from a list in Python?

How to Remove an Element from a List Using the remove[] Method in Python. To remove an element from a list using the remove[] method, specify the value of that element and pass it as an argument to the method. remove[] will search the list to find it and remove it.

How do I remove a list from a list?

We can remove a list inside a list same way like we remove the single list, just provide the reference of the list object to the del function. del[list_object].

How do I subtract a list from another list?

Let's take a look at what we've done here:.
We loaded our two lists, list1 and list2 and converted them into numpy arrays, array1 and array2..
We then used the np. subtract[] method to subtract the two lists..
Finally, since we want to return a Python list, we used the list[] method..

How do I remove a value from a list based on another list?

Method #3 : Using remove[] remove[] can also perform this task but only if the exception of not getting specific elements is handled properly. One can iterate for all the elements of the removed list and remove those elements from the original list.

Chủ Đề