Linked Questions

156 votes
7 answers
226k views

Remove all values within one list from another list? [duplicate]

I am looking for a way to remove all values within a list from another list. Something like this: a = range(1,10) a.remove([2,3,7]) print a a = [1,4,5,6,8,9]
  • 2,627
40 votes
7 answers
419k views

AND/OR in Python? [duplicate]

I know that the and and or expressions exist in python, but is there any and/or expression? Or some way to combine them in order to produce the same effect as a and/or expression? my code looks ...
  • 1,426
18 votes
1 answer
48k views

Subtracting one list from another in Python [duplicate]

What I want to happen: When given two lists (list a and list b), remove the numbers in list a that are in list b. What currently happens: My first function works only if list a has only one number to ...
user avatar
8 votes
2 answers
23k views

remove elements in one list present in another list [duplicate]

I have list1 and list2. list2 is a group of words that have to be removed from list1, for example: list1=['paste', 'text', 'text', 'here', 'here', 'here', 'my', 'i', 'i', 'me', 'me'] list2=["i","me"]...
  • 1,863
2 votes
3 answers
6k views

Remove items from list if it exists [duplicate]

My code will not delete the items if they exist in the list. This is the list I am working with: [1, 2, 3, 4, 5, 6, 7, 8] Run the function: remove_items_from_list(my_list, [1,5,6]) This is the ...
3 votes
1 answer
5k views

Removing items from a list by comparing with a second list, Python [duplicate]

A small example. I have two lists with numbers, ra and dec here. I have a third list that also has some numbers, quad here. What I want to do is to remove those values of ra and dec that are in quad....
  • 9,095
0 votes
4 answers
133 views

how do you remove a list from another list? [duplicate]

I have something like: a = [4,3,1,6,3,5,3] b = [4,2,6] and I wanted to remove the 3 elements of b from a. I was trying to do: c = a - b but I was thinking that the inverse of merge (+) might not ...
1 vote
2 answers
341 views

Filtering/removing items based on a list of strings [duplicate]

I would like to remove the words of each nested list that appear in the garbage list. List to clean: main_list = [['CV', 'Bob', 'Jones', '20201027'], ['Resume', 'Andrea', 'Jones'], ['Claire', 'Jonson',...
  • 674
-1 votes
1 answer
854 views

Get unique string from two lists python [duplicate]

I want to get unique string from two list. i tried this Get only unique elements from two lists python but it is working for only numbers a = ['st','ac','vf'] b = ['st'] output required: c =['ac',...
  • 1,760
0 votes
4 answers
504 views

How to remove all elements from List A that in List B [duplicate]

Hello I created two lists as the following: listA = ['A', 'B','C','D','1','2','3', 'A', 'D','2'] listB = ['A', 'B','C','D'] I want to remove all listB elements that are in listA. I did this: ...
2 votes
4 answers
83 views

Delete element in list [duplicate]

I have a master list which regroups every ID I have. master = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Among all of these ID, I want to exclude some of them. So I did a second list of ID which have to be ...
user avatar
-4 votes
3 answers
737 views

how to remove character in one string if present in another Python [duplicate]

I'm trying to create a function to remove characters from one string if present in another. for example: l1 = ['a', 'b', 'c', 'd', 'e', 'f'] l2 = ['b', 'c', 'e'] I want it to return ['a', 'd', 'f']...
-4 votes
2 answers
322 views

Python modify one list based on another list [duplicate]

I have a list: my_colors = ['blue', 'blue', 'blue', 'red', 'red', 'green'] And I have a 'valid' list: valid_colors = ['red', 'white', 'blue'] How can I remove any items in my list that are ...
  • 2,001
-3 votes
3 answers
90 views

Keep in array of words just given words from another array [duplicate]

Given an array: arr = ['a','b','c','d','a','b','d','f'] I would like to preprocess it with some kind of dictionary: dictionary = ['a','b','c'] so after: arr.preprocess(dictionary) all items not ...
-1 votes
4 answers
85 views

How can i create a list of elements from 2 other lists [duplicate]

Imagine i have the following 2 lists. List years- contains all years between 1901 and 2099 years=[1901,...,2099] List leap years-contais all leap years between 1901 and 2099 I want to creat a non ...

15 30 50 per page
1
2 3 4 5