0

My original thought process was that I could reference an item in a list easier by saving it in a variable.

Let's say I've created a list:

list = [0, 0 ,0 ,0, 0]

Can I store each item in a variable?

list1 = list[0]

list2 = list[1]

list3 = list[2]

list4 = list[3]

Then change that item using the variable:

list1 =+ 1

But here the original list remains the same:

print(list)

[0, 0, 0, 0, 0]

So it would appear the variable takes the value from the list, and I'm only changing the variable not the original list.

print(list1)

1

Is there a way to store list items in variables but have it so I'm actually performing operations on the items in the list, not 'copies' of the item?

codeforester
  • 39,467
  • 16
  • 112
  • 140

1 Answers1

0

Not without a lot of hacking together custom classes with overidden __dict__ values. Python assignments for primitives are pass-by-value which means that any call to x=y will always make a copy of y and any changes to x will not affect y.

Yserbius
  • 1,375
  • 12
  • 18