This question already has answers here:
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 4 years ago.
As I don't know what title should be given to my this confusion so I'm putting it just a doubt
a = [1,2,3,4,5]
b = a
for i in range(len(a)):
c = (i - 4)
print(a)
print(b)
b[c] = a[i]
print(a)
print(b)
output
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 1, 4, 5]
[1, 1, 1, 4, 5]
...
why values of list a is getting in each step of loop?
Your problem lies in this line:
b = a
This doesn't do what you think it does. In particular, it does not make a copy of a. After the assignment, both b and a refer to the same object. Thus, any change to b is reflected in a also.
One way to force a shall copy is to use the slice syntax:
b = a[:]
Related
This question already has answers here:
What is the difference between `sorted(list)` vs `list.sort()`?
(7 answers)
Closed 1 year ago.
For Example:
a = [1, 3, 5, 4, 2]
a = a.sort()
print(a)
Output:
None
a = [1, 3, 5, 4, 2]
a.sort()
print(a)
Output:
[1, 2, 3, 4, 5]
My question is why does a = a.sort() reslt in None rather than [1, 2, 3, 4, 5]? But without a= it gives me [1, 2, 3, 4, 5].
Thank you
Because, a.sort returns None, and you are overwriting a's value with that None. The list will still be sorted, because list.sort does that in place. sorted(list) does the inverse : It copies the list, sorts that copy, and returns it. You then have to assign the list to it.
This question already has answers here:
How to deep copy a list?
(10 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
What is the best way to copy a list? [duplicate]
(7 answers)
Closed 1 year ago.
Let's assume that a is some reference variable and I want to put it into a list three times:
a = [1,2,3]
b = [a]*3
>>> b
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
So this still works as expected. But if I append an element to one of the reference variables it is reflected to all of them:
>>> b[0].append(4)
>>> b
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
Does this happen because the * operator in the first code block only copies the references and the list only points to a? Do I need a for-loop in order to be able to have a list of independent reference variables (copies) n times? (where n is 3 in the example above)
How excactly does the * operator work in this instance?
With a for-loop and the append()-Method the behaviour is the same - are references also used here?:
>>> a = [1,2,3]
>>> b = []
>>> for i in range(2):
... b.append(a)
...
>>> b
[[1, 2, 3], [1, 2, 3]]
>>> b[0].append(4)
>>> b
[[1, 2, 3, 4], [1, 2, 3, 4]]
When inserting copies the behaviour is as expected:
>>> a = [1,2,3]
>>> b = []
>>> for i in range(2):
... b.append(a.copy())
...
>>> b
[[1, 2, 3], [1, 2, 3]]
>>> b[0].append(4)
>>> b
[[1, 2, 3, 4], [1, 2, 3]]
Is there a shorter, more convenient way to do this than to loop with .append() and .copy() as I did above?
My Python-Version is Python 3.8.6
Note that my questions go beyond just using deep_copy() as it is about inserting elements more often in a list and the *-operator - I know what deep_copy() is!
This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 5 years ago.
x = [4, 5, 6]
li = [1, 2, 3, 7]
li.insert(3,x)
x+=li
print(x)
The output is:
[4, 5, 6, 1, 2, 3, [...], 7]
I'm new to python/coding and I don't know what these ellipses are but when I do other code it starts getting weird. Wasn't sure what to ask since I have no clue what's going on. Thank you!
you're inserting a list inside your list, probably not what you want.
Then when doing this
x+=li
the representation of the list then shows an ellipsis because you're referencing the list from itself (x is referenced in li already)
To insert several items at once in a list in-place you could use slice assignment:
>>> x = [4, 5, 6]
>>> li = [1, 2, 3, 7]
>>> li[3:3] = x
>>> li
[1, 2, 3, 4, 5, 6, 7]
This question already has answers here:
Element-wise addition of 2 lists?
(17 answers)
Closed 7 years ago.
I'm sure there is a good way to accomplish what i want without looping over lists and creating new objects. Here is what I have
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
What I am looking to do is take each set of lists and sum each placeholder so that the output is
[3, 5, 7, 9]
Thoughts?
you should use zip function and list comprehension
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
[sum(t) for t in zip(a,b)]
Use numpy
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([2, 3, 4, 5])
a+b
>>> array([3, 5, 7, 9])
This question already has answers here:
Python list rotation [duplicate]
(4 answers)
Closed 8 years ago.
Suppose I have a list u = [1, 2, 3, 4, 5], and u[1:] returns [2, 3, 4, 5].
I wonder what indexing returns [2, 3, 4, 5, 1], going from the second position to the last and then the first?
You can make a general function that does this at any point in your list, just by adding two slices. This was an intentional design as to why slicing is half-open (includes left index, but excludes right index)
def rotate(l, i):
return l[i:] + l[:i]
>>> u = [1, 2, 3, 4, 5]
>>> rotate(u, 1)
[2, 3, 4, 5, 1]
>>> rotate(u, 2)
[3, 4, 5, 1, 2]