This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 10 months ago.
I declared an array of array by two methods:
Method 1:
bucket = [[]] * 6)
Method 2:
bucket = [[] for i in range(6)]
but while appending elements to the inner array it works diferrently.
bucket[0].append(1)
print(bucket)
the results come out to be this:
When using Method 1:
Output:
[[1], [1], [1], [1], [1], [1], [1]]
When using Method 2:
Output:
[[1], [], [], [], [], [], []]
I want to understand why this is giving me two different type of results.
So this is what happening:
when you do this bucket = [[]]*(len(nums)+1) all the nested lists
are same.
To confirm that you can print(id(bucket[0])) and
print(id(bucket[1])).
Both will print same memory address as all are same. So, when you
append value to any of the nested list it's get printed for all of
the nested list as it's same list object.
Related
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed last month.
I want to remove empty list and extra [] around elements
Input: data = [[1], [], [2], [], [], [4]];
Expected: output = [1,2,4]
When I do this : d = [x for x in data if x]
I have an output with [] around elements as below:
[[1], [2], [4]]
Appreciate if someone can help!
from itertools import chain
print(list(chain(*data)))
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
I have a list of list and I would like to add an element inside only one of the list but it is added everywhere. You can find my code bellow
repartition_labels = [[]]*3
repartition_labels[2].append(2)
The result I have is
[[2], [2], [2]]
and I would like to have the result
[[], [], [2]]
I already tried to function insert and extend but it didn't solve the problem
With repartition_labels = [[]]*3, you are generating a list which contains 3 references to the same object.
When you change that object, all references will show the (same) updated object.
You have to do this:
repartition_labels = [[] for _ in range(0, 3)]
repartition_labels[2].append(2)
Output:
[[], [], [2]]
The reason your code is not working as expected, is because by [[]] * 3 you're creating three references to the same list.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Why does appending to one list also append to all other lists in my list of lists? [duplicate]
(4 answers)
Closed 3 years ago.
I try this code below
lst = [[]] * n
lst[0].append(1)
If i print the cod, the result is
[[1], [1], [1], [1], [1], [1]]
And then I try manual way to make an empty list like below
lst = [[], [], [], [], [], []]
lst[0].append(1)
The result showing up
[[1], [], [], [], [], []]
Is there any differences between lst = [[]] * n and lst = [[], [], [], [], [], []] ?
when you multiply your list of lists with n you are repeating only the reference to the same initial list so basically you have only one list and many references to the same list and when you are modifying one element you are modifying all; when you create "by hand" you have different lists
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
It seems there is an unexpected behavior regarding lists with Python 3.8.1:
> l = [[]] * 4
> l[0].append(1)
> l
[[1], [1], [1], [1]]
I would expect l to be equal to [[1], [], [], []].
Should not the lists inside l correspond to different "objects"?
On the other side the following code behaves as expected.
> l = [ [] for i in range(4)]
> l[0].append(1)
> l
[[1], [], [], []]
This is built in behavior. When multiplying lists in such fashion, you're not creating new lists, but just copying the references to one initial list, therefore the modification of one of the items leads to others being modified.
See: List of lists changes reflected across sublists unexpectedly
As you've found, the recommended method to circumvent this issue is by using a list comprehension.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 6 years ago.
I only want to append [1] to the first list element of the main list, tree, but [1] gets appended to each list element.
tree = [[]] * 5
tree[0].append([1])
print tree
>>>[[[1], [1], [1], [1], [1]]]
What I want:
>>>[[[1]],[],[],[],[]]
The way you create the list (tree = [[]] * 5) is the problem. All the 5 lists inside the list are identical objects which you can see when using the method id():
>>> tree = [[]] * 5
>>> id(tree[0])
139859652123032
>>> id(tree[1])
139859652123032
The solution would be:
>>> tree = [[], [], [], [], []]
>>> tree[0].append([1])
>>> tree
[[[1]], [], [], [], []]
My problem was that multiplying with * creates references of the same element. I should use:
tree = [[] for i in xrange(5)]
Use a for statement to iterate and add each branch
tree = [[] for i in xrange(5)]
# xrange() and range() will have the same result, but the former is a cheaper iterator
Finally, append '[1]' to the first list
tree[0].append([1])