473,396 Members | 2,092 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,396 software developers and data experts.

What is the fastest way to replace elements in list?

I am working with very large lists. I wish to replace elements satisfying a rule as quickly as possible.

For Example:

rule: if x < 0, replace with 0

Mylist: [1, 2, -4, -3, 4, -9]

Newlist: [1, 2, 0, 0, 4, 0]

I have Mylist and want to get Newlist. My best guess is to use enumerate, but not sure how to write this appropriately. Thanks
Dec 2 '10 #1
1 2237
dwblas
626 Expert 512MB
Generally, it is faster to append to a new list. Also, take a look at the Python Style Guide when you have time. There are coding conventions that make code easier to read and understand when someone posts.
Expand|Select|Wrap|Line Numbers
  1. a_list = [1, 2, -4, -3, 4, -9]
  2. b_list = []
  3.  
  4. for el in a_list:
  5.     if el < 0:
  6.         b_list.append(0)
  7.     else:
  8.         b_list.append(el)
  9. print b_list 
Dec 2 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: danny van elsen | last post by:
hello all, I have an application in which I build a list<node>, with potentially thousands of nodes. each node has an "index", and all nodes are ordered by this index. this index reflects a...
2
by: Philip WATTS | last post by:
I have generated a dynamic option list from an array using elementCreate("OPTION") I now wish to get an event to replace this list with a new one, but of course currently, it simply adds them on...
7
by: Evangelista Sami | last post by:
Hi all i have implemented a list type as an array of pointer like this typedef struct { int nb_elements; void **elements; } list; to avoid having a pointer for each element as it is done...
4
by: juli jul | last post by:
Hello, How can I pass over all the elements that are in the first column of listview? I need to do it in a loop and to present each cell value,how? Thank you! *** Sent via Developersdex...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
4
by: jgabbai | last post by:
Hi, What is the best way to white list a set of allowable characters using regex or replace? I understand it is safer to whitelist than to blacklist, but am not sure how to go about it. Many...
1
by: eight02645999 | last post by:
hi say i have a list alist = I wanted to change the elements , say alist . If i do alist = "t" , it gives which is not what i want. I wanted alist =
1
by: [rob desbois] | last post by:
This may be a very dumb question, but brain can't seem to find anything to confirm/deny suspicion. I have an array created with: FooClass* foo = new FooClass; I want to be able to replace an...
1
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
2
by: antar2 | last post by:
I want to replace each first element in list 5 that is equal to the first element of the list of lists4 by the fourth element. I wrote following code that does not work: list4 = , , ] list5 = ...
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.