-10

I don't know how to loop over variables, assigning values to them.

a = 1; b = 2; c = 3 
for x in [a, b, c]:
  print(x)

1
2
3

for x in [a, b, c]:
  x = x * 2

for x in [a, b, c]:
  print(x)

1
2
3
gnat
  • 6,213
  • 108
  • 53
  • 73
Wagner Ikeda
  • 25
  • 1
  • 1

4 Answers4

5

You can use a list comprehension and tuple unpacking.

a = 1
b = 2
c = 3

a, b, c = [i * 2 for i in [a, b, c]]

print(a, b, c)
# 2 4 6
James Schinner
  • 1,549
  • 18
  • 28
2

When you iterate over a list, like this: for x in [a, b, c]: what you are doing is telling the python interpreter to create a variable x that is repeatedly assigned different values. These values are the values of a, b and c.

What is important to note here is, x does not reference a, b and c. That is to say, x is not a alias for a, for example. It is a different variable, that just happens to have the same value.

You can think about it like this:

There exists a person called First Middle Last. His first name is First, his middle name is Middle and his third name is Last. This First Middle Last person wears a red sweater.

Weather someone calls him First or Middle, it does not matter, both of these names refer to the same person. If you say First now wears a blue sweater and then ask What colour is Middle's sweater, the answer ought to be blue. This is because both First and Middle refer to the same person.

This however is not what is going on in your iteration. In your case, x is not to a what Middle is to First. Instead, what happens is more akin to this.

You have three persons, John, Jane and Doe ( a, b, c ). John has a blue sweater, Jane has a red sweater and Doe has a green sweater. This is how a has the value 1, b has the value 2 and c has the value 3.

One by one, you ask them, what colour their sweater is. When you find out, you then say, James, a person different from John, Jane or Doe, now has the same colour sweater. If you subsequently change the colour of James's sweater, that will not affect either John, Jane or Doe.

This is a explanation on why your code does not do what you want it to do.

As for a fix, one possible pythonic way of doing it would be like this.

map(lambda x: x*2, [a, b, c])

More generally, map(transformation, array).

Let's go over the code. If you go here, you will find the definition of map.

As you see, map is a built in function that takes a a first parameter a function. In our case, we want that function to be multiplication by two.

For that purpose, we can either do lambda x: x*2, which defines a function that accepts as argument x and returns x multiplied by two, or, we can define that function explicitly.

def multiply_by_two(x):
    return x * 2

Regardless of the choice, the idea still holds, you will give a the first argument of map a function that transforms the input in the way you desire.

The input in this case is every element of the array in turn. To make this more explicit, let's do the example line by line.

The call map(multiply_by_two, [1, 2, 3]) will transform into [multiply_by_two(1), multiply_by_two(2), multiply_by_two(3)], which in turn will turn into [1 * 2, 2 * 2, 3 * 2]. This will finally transform in [2, 4, 6], the result of the map(multiply_by_two, [1, 2, 3]) invocation.

I hope this will help you understand python better.

mayk93
  • 1,489
  • 3
  • 17
  • 31
1

Use can use dictionary and loop over keys, and easily assign values to the corresponding keys' values.

Dev Anand
  • 64
  • 5
0

This should do the trick:

abc = [1, 2, 3]

for i in range(len(abc)):
    abc[i] *= 2
wohe1
  • 755
  • 7
  • 26