for(each) element aliasing

M

Matija Papec

I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

and get the result
 
F

F. Petitjean

I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

For instance :
arr = 1, 2, 3 # range(1, 4)
arr = [ element*2 for element in arr ]
print arr # [2, 4, 6] it is a list
for element in arr:
print element, # note the comma
print # useful if not in interactive mode only


Read the documentation about "list comprehension"
 
W

Will McGugan

Matija said:
I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

and get the result

Something like this?

arr= range(1,4)
for index, value in enumerate( arr):
arr[index]*= 2

print arr

Regards,

Will McGugan
 
R

Reinhold Birkenfeld

Will said:
Matija said:
I know how to alter the list via list indicies but I would like to know if
there is posibility to write in Python something like:

my @arr = 1..3;
for my $element (@arr) {
$element *= 2;
}
print "@arr\n";

and get the result

Something like this?

arr= range(1,4)
for index, value in enumerate( arr):
arr[index]*= 2

But for this you won't need enumerate:

One of
for index in range(len(arr)):
for index in xrange(len(arr)):

is sufficient.

Reinhold
 
I

Istvan Albert

Reinhold said:
But for this you won't need enumerate:
for index in range(len(arr)):

Isn't the range(len(arr)) construct somewhat of an
contraption? It has become a recognizable pattern
of course but that was by necessity rather than
correctness.

Istvan.
 
M

Matija Papec

X-Ftn-To: Jp Calderone

Jp Calderone said:
No. Integers in Python are immutable. For lists of other things, mutation in-place (similar to the above example) is possible, but for integers and other immutable types, it is not. You may be interested in list comprehensions, though:

arr = range(1, 4)
arr[:] = [a * 2 for a in arr]
print arr

I guess this works like "map" but "for" is more idimatic.


Thank you all for so quick responses. :)
 
R

Reinhold Birkenfeld

Matija said:
X-Ftn-To: Jp Calderone

Jp Calderone said:
No. Integers in Python are immutable. For lists of other things, mutation in-place (similar to the above example) is possible, but for integers and other immutable types, it is not. You may be interested in list comprehensions, though:

arr = range(1, 4)
arr[:] = [a * 2 for a in arr]
print arr

I guess this works like "map" but "for" is more idimatic.

Yes. List comprehensions are elegant (and fast) expressions you would
otherwise construct with map() and filter().

Reinhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top