eeskyy
eeskyy

Reputation: 27

Value is not changing even after appending. Python

I'm very new to python and I just don't understand why the value of 'a' doesn't change even after 'orange' has been appended. len(x) becomes 4 but 'a', which is supposed to be len(x) stays 3. Thankyou for your time.. code-

x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

x.append('orange')

print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

The output

Upvotes: 0

Views: 700

Answers (5)

ahrooran
ahrooran

Reputation: 1115

Most answers explained the how part. Let me explain why python behaves like this.

Python like all other languages reads a script from top to bottom. That means whatever it stored from or changed in the upper part of code stays the same if you don't explicitly change.

Consider the first part of your code:

x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

In this case

  • on first line python first create an array of 3 elements. Then assign the array to a reference x. Note that arrays are mutable (meaning they can be altered in place with the code written below of array creation)
  • on the second line python computes length of x (in this case 3) and assigns the computed integer (3) to a reference a. Note that integers are immutable (meaning you cannot alter the value in place below). Also note that after python assigns the value it forgets where did the integer come from. (meaning python only knows there is an integer. it forgets the fact that this integer represents the length of array)
  • on third line you print x which prints the array referenced by x
  • on fourth line you print the length of array. Python again computes the length and prints it (it does not print the value referenced by a)
  • on fifth line you print a. (this prints the value printed by a)

Now consider the second part of your code:

x.append('orange')

print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
  • When you append a new element, since lists are mutable, 'orange' is added in last of list.
  • Now you print x. So python retrieves the list (which is now modified) and prints it
  • Now you print length of x. Python again computes the length of x (which would be 4) and prints the length
  • Now you print a. The value referenced by a is still 3. Since you haven't explicitly changed a, and since python already forget how the value of a is calculated in the first place, python would simply return the initial value it calculated above.

To rectify this, you could reassign a like this:

a = len (x)

Now python would recalculate the length of modified list and assign it again to a. The problem here is an integer is immutable. Hence python would calculate the new value (4); store it in RAM; and reference a to point that value (4). Since the original value (3) is not going to be referenced anymore, that value (3) will be discarded by python. (Eventually removed from RAM by garbage collector)

Upvotes: 1

Saaransh Garg
Saaransh Garg

Reputation: 156

you need to change the value of a after you have changed the length of the list. a still remains 3 until and unless you say a= len(x) again. this would change its value to 4

appended code:

x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

x.append('orange')

a = len(x) #this has to be done

print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

Upvotes: 1

Luc Bertin
Luc Bertin

Reputation: 336

a variable refers to len(mylist) : the length of the first list (before any modifications).

It points to the object returned by len(mylist) which is an integer (immutable).

Lists are mutable, hence you can modify them inplace. But a still refers to the previous integer from len(mylist). Hence you have to call again len(mylist) after you modified mylist and assign it back to a so a get updated.

Upvotes: 1

Zander Kraig
Zander Kraig

Reputation: 11

The answer is pretty simple:

x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

x.append('orange')
a = len(x) # Note this extra line.

print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

The extra line serves to change the value of a. In the beginning, a = len(x) makes a = 3 since len(x) = 3. However, after len(x) becomes 4, we must again assign the value of len(x) to a (that is make a = len(x)). Not doing this means the variable a stays stuck at 3.

Python programs and almost all other programming languages run top to bottom. Once, python runs a particular line, it will keep moving downwards and not repeat that line (unless you specifically ask the Python interpreter to do so).

Upvotes: 1

user14229275
user14229275

Reputation:

i dont have enough reputations to comment so : you must change the value of a varrible then the value will be changed.

x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

x.append('orange')


#Add this to change for example:
a = len(x)

print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)

Upvotes: 3

Related Questions