Python lists being edited in place? [duplicate] - python

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Is i = i + n truly the same as i += n? [duplicate]
(3 answers)
Closed 2 years ago.
So this is happening:
a = [1,2]
b = a
b += [3]
print(a)
[1, 2, 3]
It's extremely confusing and it's screwing up my project's test suite, which takes a list of strings and tests that it gets modified in the correct ways by each function.
Did I just find a bug in Python, or is there some convoluted reason for this misbehavior?

Related

List assigning to another variable, then original variable also changes when there will happen an operation on child variable [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed last month.
a = [1,2,4]
b=a
b.insert(1,45)
print(a,b)
results
a = [1,45,2,4]
b = [1,45,2,4]
why a is changing
is there any way, where b will change
That's because you're just reassigning the same instance. You need to do this instead of b=a:
b = a.copy()

Why is this code printing True and not False? (Is it due to matrix creation?) [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 4 years ago.
image=[[1,1,1],[1,1,0],[1,0,1]]
visited = [[False] * len(image[0])] * len(image)
visited[0][0] = True
print((visited[1][0]))
Shouldn't the above python code print False? Why is the entire column being assigned as True?
The reference to [False] * len(image [0]) is repeated len(image) times, so modifying the first element of visited affects the second (and third).

Python is operator [duplicate]

This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.

Is a Python list's += operator equivalent to append() or extend()? [duplicate]

This question already has answers here:
Concatenating two lists - difference between '+=' and extend()
(11 answers)
Python append() vs. + operator on lists, why do these give different results?
(7 answers)
Closed 6 years ago.
Python lists have a += operator as well as append and extend methods.
If l is a list, is l += ... equivalent to l.append(...), l.extend(...), both, or neither?
In python += on a list is equivalent to extend method on that list.

Python: What is the `is` function? [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 9 years ago.
This might be a stupid question, but what exactly is the is function, and when would one use it?
From the context, i guess i could infer that it's equivalent to ==; but if that's the case, why have both? The Built-in Functions Reference shows nothing, and help(is) returns a SyntaxError.
is checks if the objects have the same identity. == only checks if they are equal.
>>> L1 = [1,2,3]
>>> L2 = [1,2,3]
>>> L1 is L2
False
>>> L1 == L2
True

Categories

Resources