Remove elements from a list, using another list as indices
I have a list primeList and another list ls . primeList is a list full of integers, and I need to delete the values from ls which have an index that is in primeList . For example if primelist = [1, 3 , 5] and ls = [1, 2, 3, 4, 5, 6, 7] , then indexes 1, 3, and 5 should be removed from ls , making ls = [1, 3, 5, 7] At the moment I’m trying to use this bit of code:
primeList = list(getPrimes()) ls = list(ls) for i in primeList: del ls[i] return ls
Traceback (most recent call last): File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 26, in otherList = delPrimes(myList) File "C:/Python34/My Scripts/NLP lab2/exec2.py", line 18, in delPrimes del ls[i] IndexError: list assignment index out of range`
I believe this is because getPrimes is a larger list than ls, but I’m not sure how to work around this problem in Python? EDIT — This is all of my current code:
def delPrimes(*ls): def getPrimes(): L = [] for x in range(2, 230): isPrime = True for y in range(2, x): if x % y == 0: isPrime = False if isPrime: L.append(x) return L primeList = list(getPrimes()) ls = list(ls) for i in primeList: del ls[i] return ls myList = list(range(1, 51)) print(myList) print("--" * 40) otherList = delPrimes(myList) print(otherList)
As part of some schoolwork we need to «Write a method in Python to delete the items at prime index locations in a list (up to index location 50). E.g. It will remove the item at index location 2, 3, 5, 7, … » I also believe we must use ‘del’ to do the deletion. EDIT2:
for i in reversed(primeList): if i
Remove all values within one list from another list? [duplicate]
What if I've a list [1,2,2,2,3,4] and a sublist [2,3] , then the result should be [1,2,2,4] , is there a Pythonic way to do that?
@user this gets you most of the way there - but your problem is a different problem! l=[1,2,2,3,4] sl=[2,3] [x for x in [l[n:n+2] for n in range(0,len(l))[::2] ] if x != sl]
I was looking for fast way to do the subject, so I made some experiments with suggested ways. And I was surprised by results, so I want to share it with you.
Experiments were done using pythonbenchmark tool and with
a = range(1,50000) # Source list b = range(1,15000) # Items to remove
def comprehension(a, b): return [x for x in a if x not in b]
5 tries, average time 12.8 sec
def filter_function(a, b): return filter(lambda x: x not in b, a)
5 tries, average time 12.6 sec
def modification(a,b): for x in b: try: a.remove(x) except ValueError: pass return a
5 tries, average time 0.27 sec
def set_approach(a,b): return list(set(a)-set(b))
5 tries, average time 0.0057 sec
Also I made another measurement with bigger inputs size for the last two functions
a = range(1,500000) b = range(1,100000)
For modification (remove method) - average time is 252 seconds For set approach - average time is 0.75 seconds
So you can see that approach with sets is significantly faster than others. Yes, it doesn't keep similar items, but if you don't need it - it's for you. And there is almost no difference between list comprehension and using filter function. Using 'remove' is ~50 times faster, but it modifies source list. And the best choice is using sets - it's more than 1000 times faster than list comprehension!
Remove List From List in Python
- Remove List B From List a Using the remove() Method in Python
- Remove List B From List a Using the difference() Method in Python
The list in Python is a data structure that contains an order sequence of items. We can perform many operations on the list. Let’s say we want to remove a list B from list A. It simply means that we want to remove the items from list A that are also present in list B.
For Example we have a list A which contains items ["Blue", "Pink", "Purple", "Red"] and List B contains the items ["Silver", "Red", "Golden", "Pink"] . Now, if we remove list B from list A, in the output, we will get list A as ["Blue", "Purple"] because these items were also present in list B. We can do this task either by using the remove() function with a list or using the difference() function available with the set data structure.
Remove List B From List a Using the remove() Method in Python
In this example, we will use the remove() method on list A to remove the items that are similar in list A and list B. We use the remove() method with list A so that items will be removed from list A, but list B will be the same as before. In this code, we iterate over the items of list A and check if that item is also present in list B; the item will be removed from list A.
# Python 3.x list_A = ["Blue", "Pink", "Purple", "Red"] list_B = ["Silver", "Red", "Golden", "Pink"] print("List A before:", list_A) print("List B before:", list_B) for item in list_A: if item in list_B: list_A.remove(item) print("List A now:",list_A) print("List B now:",list_B)
List A before: ['Blue', 'Pink', 'Purple', 'Red'] List B before: ['Silver', 'Red', 'Golden', 'Pink'] List A now: ['Blue', 'Purple'] List B now: ['Silver', 'Red', 'Golden', 'Pink']
Remove List B From List a Using the difference() Method in Python
Another way to remove similar items from list A is to subtract them from list B. With the set data structure, there is a method difference() that will return the items present in set A but not in set B. It returns only the different items of set A, which are unique between the two sets. But as this method is available with set .
So in our code, we will first cast both lists to set, then apply the set_A.difference(set_B) function, and we will store the result again in list_A by casting the result to list data type.
# Python 3.x list_A = ["Blue", "Pink", "Purple", "Red"] list_B = ["Silver", "Red", "Golden", "Pink"] print("List A before:", list_A) print("List B before:", list_B) setA = set(list_A) setB = set(list_B) list_A = list(setA.difference(list_B)) print("List A now:",list_A) print("List B now:",list_B)
List A before: ['Blue', 'Pink', 'Purple', 'Red'] List B before: ['Silver', 'Red', 'Golden', 'Pink'] List A now: ['Purple', 'Blue'] List B now: ['Silver', 'Red', 'Golden', 'Pink']
I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.