0

Suppose I have two lists listBig and listSmall and I want to delete all elements of listSmall from listBig. Assume that all elements of listSmall are present in listBig and that if there are multiple instances of an element of listSmall in listBig, they must not be deleted, meaning deleting one is the key.

Is there a better way to achieve this than:

listBig = [1,2,3,4,5,6,7,8,4,7]
listSmall = [4,7]
deleted = 0
inner = 0
for outer in range(0,len(listSmall)):
    while not deleted == len(listSmall):
        if listSmall[outer]==listBig[inner]:
            listBig.remove(listBig[inner])
            inner=0
            deleted+=1
            break
        inner+=1
print listBig

OUTPUT: [1, 2, 3, 5, 6, 8, 4, 7]
4
  • modified the lists to show that if there are elements in listSmall which occir more than once in listBig, multiple occurrences MUST NOT BE DELETED. Thanks. Sorry for creating some confusion. Commented Nov 25, 2014 at 21:48
  • Do you have ordering constraint? Why do you remove one of the duplicated element in listBig?@user1989682 Commented Nov 25, 2014 at 21:54
  • No. There are no ordering constraints. Commented Nov 25, 2014 at 21:57
  • The big list was created by adding elements from multiple small lists. I want to "undo" the effect of one of the small lists. The duplicates were added by other small lists. So they must stay. Commented Nov 25, 2014 at 22:07

4 Answers 4

2

try:

listBig = [item for item in listBig if item not in listSmall]
Sign up to request clarification or add additional context in comments.

1 Comment

if any list has duplicated elements, set(list_nname) would only keep one left
1

I would convert your listSmall to a set (for faster membership lookup), then you can use a simple list comprehension

>>> setSmall = set(listSmall)
>>> listBig = [i for i in listBig if i not in setSmall]
>>> listBig
[1, 2, 3, 5, 6, 8]

5 Comments

if you change listSmall to set() might be better if listSmall is a long list and there is no duplicated element in listSmall. Because in works for set() in constant time, while it does linear time in list @Cyber @user1989682
@ChesterL I'm not sure I understand your point. As you said the in operation is faster for set than for list. For any decent sized listSmall, it would be beneficial to make a set.
Thanks. I am sorry for creating some confusion. I realised that in my case, if there are multiple occurrences of an element of listSmall in listBig, they MUST NOT BE deleted. I will edit the question.
@Cyber As long as there is no repeated element in listSmall, otherwise, set() will keep only one left
Please check the modified question.
0

If you don't want to remove the duplicates, list.remove(x) only removes the first element it finds in the list.

    listBig = [1,2,3,4,5,6,7,8,4,7]
    listSmall = [4,7]
    for item in listSmall: listBig.remove(item)

Comments

0

You can use list comprehension to compare every element in both lists and check whether there are any repeated elements. If so, the repeated element will be deleted from listBig

>>>'listBig[:] = [element for element in listBig if element not in listSmall] >>>print listBig

[1, 2, 3, 5, 6, 8]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.