python remove elements from list based on other list [duplicate] - python

This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 6 years ago.
I'm stuck with a part in one of my codes where I have to delete all the occurances present in listA that are identical in listB.
Example:
A=[1,4,4,4,3,3,2,1,5,5]
B=[4,3]
Result should be A=[1,2,1,5,5]. Ideally I would want to do it in linear time.

using Set Operations:
list(set(A) - set(B))
Using List Comprehension
list(set([i for i in A if i not in B]))

Try with list comprehension,
In [11]: [i for i in A if i not in B]
Out[11]: [1, 2, 1, 5, 5]

Related

Compare multiple lists inside a list Python [duplicate]

This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 4 years ago.
Is it possible to compare a list containing an unknown number of lists with equal elements in a more terse (aka shorter) manner than what I have done? Preferably an one-liner!
Here's an example if it's unclear what I want to do:
a = [1, 2, 3]
b = [4, 2, 1]
c = [7, 5, 1]
d = [a, b, c]
def multiCompList(lists):
final = [i for i in lists[0] if i in lists[1]]
for i in range(2, len(lists)):
final = [i for i in final if i in lists[i]]
return final
print(multiCompList(d))
What I've done is to first check if the first and second list contains any equal elements and put them in a list called final. Thereafter, checking if those elements can be found in the lists after and replacing the final-list with the remaining equal elements.
The results in this case is: [1].
A oneliner would look like this:
set.intersection(*[set(x) for x in d])
#set([1])

Delete specific elements from a list in python 3 [duplicate]

This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Deleting multiple elements from a list
(32 answers)
Closed 4 years ago.
I have a list of values for example:
[1,2,3,4,5,6,7,8,9,10]
And specific indexes like:
[0,3,5]
I want something that returns deleting the values which the index belongs to the [0,3,5] array:
[2,3,5,7,8,9,10]
Any ideas for Python 3?
Thanks
Using enumerate and list comprehension.
Ex:
l = [1,2,3,4,5,6,7,8,9,10]
toDelete = [0,3,5]
print([v for i,v in enumerate(l) if i not in toDelete])
Output:
[2, 3, 5, 7, 8, 9, 10]

list.remove method doesn't work properly [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Removing from a list while iterating over it [duplicate]
(5 answers)
Closed 5 years ago.
I have the next piece of code which counts unique elements in list and remove those elements from that list:
l = [1, 2, 3, 4, 5]
for i in l:
if l.count(i) == 1:
l.remove(i)
print l
However, after list modification by l.remove(i) I still have elements in the list: [2, 4]
My question is why list.remove() operation doesn't delete all elements if all of them are unique and amount per each one will be 1 during each iteration?

Ternary operator in enumeration loop [duplicate]

This question already has answers here:
list comprehension with multiple conditions (python)
(2 answers)
Closed 6 years ago.
In my code, I'm trying to loop through an enumeration object and selectively add some values from the enumeration to a new list using a list comprehension.
This works:
a = [1, 2, 3, 4, 5]
s = [i[1] for i in enumerate(a)]
Of course, that basically just copies list a over to s.
This doesn't work (a being the same list):
s = [i[1] if i[1] != 2 for i in enumerate(a)]
I would think that this would just copy every element of list a over to s besides the 2, but instead I get a syntax error. Anybody know what's going on here?
You misplaced the if part:
s = [i[1] for i in enumerate(a) if i[1] != 2]

Remove duplicates and finding unique sublists [duplicate]

This question already has answers here:
Get unique elements from list of lists when the order of the sublists does not matter
(2 answers)
Closed 9 years ago.
I have a nested list that looks like this:
lst = [[1,2,3],[1,2],[1,2,3],[2,3],[4,5],[2,3],[2,4],[4,2]]
I would like to find the unique sublists in lst. Using the above example, I'd like to find:
lst_set = [1,2,3],[1,2],[2,3],[4,5],[2,4]]
Order does not matter. In otherwords, [2,4] and [4,2] are the same.
set(tuple(sorted(i)) for i in lst)
In [22]: lst = [[1,2,3],[1,2],[1,2,3],[2,3],[4,5],[2,3],[2,4],[4,2]]
In [23]: set(frozenset(item) for item in lst)
Out[23]:
set([frozenset([2, 4]),
frozenset([1, 2]),
frozenset([2, 3]),
frozenset([1, 2, 3]),
frozenset([4, 5])])
Take a look at the built-in set() function. As the list is unhashable, you might need to turn the lists into a tuple before using set().
That means:
set([tuple(sorted(x)) for x in lst])

Categories