473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

removing all instances of a certain value from a list

Hi,
I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
many missing vlaues which are represented as None. I would like to
remove all such instances in one go.
There is a remove function but it removes only the first instance, is
there a delete/remove all function?
thanks
Mar 19 '08 #1
4 3040
On Wed, Mar 19, 2008 at 10:28 PM, Lee Sander <le*****@gmail.comwrote:
Hi,
I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
many missing vlaues which are represented as None. I would like to
remove all such instances in one go.
There is a remove function but it removes only the first instance, is
there a delete/remove all function?
thanks
If it is ok to copy the list instead of mutating it, use a list comprehension:
>>L = [-1.3, 1.22, 9.2, None, 2.3]
[x for x in L if x is not None]
[-1.3, 1.22, 9.1999999999999993, 2.2999999999999998]

--
mvh Björn
Mar 19 '08 #2
Lee Sander <le*****@gmail.comwrites:
Hi,
I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
many missing vlaues which are represented as None. I would like to
remove all such instances in one go.
There is a remove function but it removes only the first instance, is
there a delete/remove all function?
No, but you can easily simulate it using, for example:

lst[:] = (el for el in lst if el is not None)
Mar 19 '08 #3
On Mar 19, 11:28 pm, Lee Sander <lesa...@gmail.comwrote:
Hi,
I have a float array ( eg [-1.3, 1.22, 9.2, None, 2.3] ) but there are
many missing vlaues which are represented as None. I would like to
remove all such instances in one go.
There is a remove function but it removes only the first instance, is
there a delete/remove all function?
thanks
You can also do it with the filter function.
>>a= [-1.3, 1.22, 9.2, None, 2.3]
a=filter ( lambda b: b != None, a)
print a
[-1.3, 1.22, 9.1999999999999993, 2.2999999999999998]

Greetings Rainer
Mar 21 '08 #4
r.gr...@science-computing.de:
>a=filter ( lambda b: b != None, a)
With None it's better to use is/not is instead of ==/!=

Bye,
bearophile
Mar 21 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: anon | last post by:
I'm aware that you can assign a value to an attribute in all class instances by assigning to <Class>.<attribute>, however, my case is slightly different and bizarre. In module node: top,...
2
by: Torsten Bronger | last post by:
Hallöchen! When I add a warning filter with warnings.filterwarnings, how can I get rid of it? I've read about resetwarnings(), but it removes all filters, even those that I didn't install in a...
4
by: Chad Myers | last post by:
I'm instrumenting my app with a few performance counters and I'd like to ask you all for some advice on how to handle performance counter instances. I have a class library that is a base library...
0
by: Frawls | last post by:
Hi, This is concerned with System.Web.UI.WebControls.DropDownList I am having problems creating a method which will remove list items from a preloaded dropdownlist. This dropdown is loaded...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
4
by: sri2097 | last post by:
Hi all, I'm storing number of dictionary values into a file using the 'cPickle' module and then am retrieving it. The following is the code for it - # Code for storing the values in the file...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
3
by: Yobbo | last post by:
Hi All I have an ASP function in place to strip invalid chars out of a data store before I create an XML file of this data, but my function doesn't work on a certain set of chars. As far as I...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.