How to remove a specific element from an array using python

I want to write something that removes a specific element from an array. I know that I have to for loop through an array to find the element matching the content.

Let's say I have an array of emails and I want to get rid of the item that matches some email line.

I would like to use a for loop structure because I need to use the same index for other arrays.

Here is the code I have:

 for index, item in emails: if emails[index] == 'something@something.com': emails.pop(index) otherarray.pop(index) 
+90
python arrays
Aug 19 '11 at 7:27
source share
5 answers

You do not need to iterate over the array. Simply:

 >>> x = ['ala@ala.com', 'bala@bala.com'] >>> x ['ala@ala.com', 'bala@bala.com'] >>> x.remove('ala@ala.com') >>> x ['bala@bala.com'] 

This will delete the first event corresponding to the row.

EDIT: after editing, you still don't need to iterate. Just do:

 index = initial_list.index(item1) del initial_list[index] del other_list[index] 
+130
Aug 19 '11 at 7:28 a.m.
source share

Using filter() and lambda will provide a neat and accurate method to remove unwanted values:

 newEmails = list(filter(lambda x : x != 'something@something.com', emails)) 

This does not change emails. It creates a new list newEmails containing only those elements for which the anonymous function returned True.

+9
Nov 10 '16 at 16:44
source share

An easy way to do this is to use zip() and an expression to represent / generate the list:

 filtered = ( (email, other) for email, other in zip(emails, other_list) if email == 'something@something.com') new_emails, new_other_list = zip(*filtered) 

Also, if you are not using array.array() or numpy.array() , then most likely you are using [] or list() , which give you lists, not arrays. Not the same.

+2
Aug 19 '11 at 8:30
source share

Your for loop doesn't work if you need an index in a for loop:

 for index, item in enumerate(emails): # whatever (but you can't remove element while iterating) 

In your case, Bogdan’s solution is fine, but the choice of data structure is not so good. The need to maintain these two lists with data from one associated with the data of the other with the same index is awkward.

A list of buffers (email, otherdata) may be better, or a dict with an email key.

+1
Aug 19 2018-11-11T00:
source share

There is an alternative solution to this problem, which also applies to duplicate matches.

We start with 2 lists of the same length: emails , otherarray . The goal is to remove items from both lists for each index i where emails[i] == 'something@something.com' .

This can be achieved using list comprehension and then splitting through zip :

 emails = ['abc@def.com', 'something@something.com', 'ghi@jkl.com'] otherarray = ['some', 'other', 'details'] from operator import itemgetter res = [(i, j) for i, j in zip(emails, otherarray) if i!= 'something@something.com'] emails, otherarray = map(list, map(itemgetter(0, 1), zip(*res))) print(emails) # ['abc@def.com', 'ghi@jkl.com'] print(otherarray) # ['some', 'details'] 
0
May 14 '18 at 14:31
source share



All Articles