python list: append vs += [duplicate] - python

This question already has answers here:
What is the difference between Python's list methods append and extend?
(20 answers)
Why does += behave unexpectedly on lists?
(9 answers)
Closed 4 years ago.
For Python list, is append() the same as +=?
I know that + will lead to the creation of a new list, while append() just append new stuff to the old list.
But will += be optimized to be more similar to append()? since they do the same thing.

It's an __iadd__ operator. Docs.
Importantly, this means that it only tries to append. "For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y."
This thread specifically deals with lists and their iadd behavior

Related

Lambda in list comprehension returning effectively the same lambda [duplicate]

This question already has answers here:
Local variables in nested functions
(4 answers)
Lambda in a loop [duplicate]
(4 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 2 years ago.
I want to generate the Lagrange basis polynomials for a set of points where T is the set of points and n is the amount of given points. What I did is ci = [lambda t: np.prod([(t - T[j]) / (T[i] - T[j]) for j in range(n) if j != i] for i in range(n)], however, the resulting lambdas are all the same.
I checked by printing [c(val) for c in ci] in the console for several different values and it all returns the same value. How is this possible, when clearly the formula I put in should return lambdas with a different structure for each i?

Python lists being edited in place? [duplicate]

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?

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).

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.

Does python store one value for an int and many references? [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 7 years ago.
I'm wondering why this evaluates to True.
x = 1
if x is 1:
print "Does x and 1 point to the same object?"
print "Does this mean python doesn't store redundant values?"
It doesn't work for this case as I expect.
x = range(10)
y = range(10)
if not x is y:
print "I expect this"
My understanding is that is checks to see if two names are pointing to the same object. Does this imply that python has a mechanism to avoid creating redundant values?
This is an implementation detail of the CPython interpreter, but integers with small values are "interned" -- whenever the result of an expression falls within a certain range, an existing int object with the same value is returned.
You could check the range that gets interned by using the following code:
interned_range = [i for i in range(-1000, 1000) if i+0 is i]
print min(interned_range), max(interned_range)
My CPython inters integers between -5 and 256 inclusive.
Being an implementation detail, this behaviour should generally not be relied upon.

Categories

Resources