Add element in a list in a list [duplicate] - python

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.

Related

How to remove empty lists from a list and extract values using python [duplicate]

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

Diferrence in inititializing array in python by these methods [duplicate]

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.

Strange behavior when initializing a list of lists in Python 3 [duplicate]

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.

Python appending to a sublist in a list of lists [duplicate]

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], [], []]

Creating a multidimensional list and appending element[] [duplicate]

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

Categories