473,386 Members | 1,706 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,386 software developers and data experts.

How do I change elements in a list?

How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:
- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]
I need to change a list, in place, so other variables referencing that
list also see the change.
Thank you...
Nov 6 '07 #1
3 2422
On Nov 6, 3:11 pm, "Just Another Victim of the Ambient Morality"
<ihates...@hotmail.comwrote:
How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:

- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]

I need to change a list, in place, so other variables referencing that
list also see the change.
Thank you...
You can "point" one variable name at another so that they both
reference the same list:

second = first

To change a list "in place", I usually do something like this:

first[someElement] = something-else

or

# change the first element in the list
first[0] = 55

See also

http://docs.python.org/tut/node7.html
http://www.diveintopython.org/native...pes/lists.html
http://docs.python.org/lib/built-in-funcs.html

Mike

Nov 6 '07 #2
Dan
On Nov 6, 4:11 pm, "Just Another Victim of the Ambient Morality"
<ihates...@hotmail.comwrote:
How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:

- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]

I need to change a list, in place, so other variables referencing that
list also see the change.
Thank you...
>>first = [1,2]
second = first
for i in range(len(first)):
first[i] += 1
>>first
[2, 3]
>>second
[2, 3]

-Dan

Nov 6 '07 #3
Just Another Victim of the Ambient Morality a écrit :
How do you change certain elements in a list? I'm looking to do the
Python equivalent of this Ruby code:
- first = [1, 2]
= [1, 2]
- second = first
= [1, 2]
- first.map! {|i| i + 1}
= [2, 3]
- first
= [2, 3]
- second
= [2, 3]
I need to change a list, in place, so other variables referencing that
list also see the change.
first = [1,2]
second = first
first[:] = [i+1 for i in first]

HTH
Nov 6 '07 #4

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

Similar topics

1
by: Matthias Teege | last post by:
Moin, how do I change the list-style-type in nested unordered lists depending on the level? Many thanks Matthias
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...
0
by: Alan | last post by:
Hi all, Before the invoke list of one delegate is called, is thers one general method to go inside the procedure in order to make some changes such as removing some delegates from the list. If so,...
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...
0
by: mccoyn | last post by:
I am making a class dervied from ComboBox. The height of the items in the list will be changed by the program. I've gotten this to work with OnMeasureItem, but there is a large space left behind. ...
3
by: manstey | last post by:
how do I detect a change in a list of class instances? from copy import deepcopy class CaListOfObj(list): """ subclass of list """ def __init__(self, *args, **kwargs): list.__init__(self,...
6
by: python101 | last post by:
I have a list of integers s= to be converted to s= I tried s= for i in s: str(a) '2'
7
by: ethanhines | last post by:
I am using MS Access 2007, I want to change the size of the Check Box that is displayed when a Value List is displayed for multiple selection. I changed the font size but I need to change the Check...
3
by: Alan J. Salmoni | last post by:
Hello everyone, I searched through groups to find an appropriate answer to this one but could only find these which didn't meet my program's needs:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.