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
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 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.
This question already has an answer here:
Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
(1 answer)
Closed 4 years ago.
>>> f = [[]] * 3
[[], [], []]
>>> f[0].append(1)
[[1], [1], [1]]
I think [[]] * x creates a list of x lists, but each inner list is the same object so changing one inner list changes all the others.
How can I quickly initialise a list of lists, but have each inner list be unique?
I want
>>> f = something
[[], [], []]
>>> f[0].append(1)
[[1], [], []]
Use
f = [[] for _ in range(3)]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 5 years ago.
I have a list of empty lists as such:
some_lists = [[]] * 3
[[], [], []]
I want to append an integer to the first sublist:
some_lists[0].append(1)
But it appends the integer 1 to all the sublists and generates:
[[1], [1], [1]]
How do I append elements individually to the sublists?
You have created a reference to every sublist in the list. Instead, you can use a list comprehension:
some_lists = [[] for i in range(3)]
some_lists[0].append(3)
Output:
[[3], [], []]
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])