Then -
What you want to do is confusing.
How you are doing is confusing (you put a "like this" code but it does not have everything that is going on) - and you could have given some concrete example of how your data gets.
But lastly, the code snippet you should not present the problem you describe.
Your problem is this: you are inserting the same object (a list) as an item in a list in more than one place.
If you do this in Python:
In [86]: a = ["a", ]
In [87]: b = [a, a]
In [88]: b
Out[88]: [['a'], ['a']]
In [90]: a.append("b")
In [91]: b
Out[91]: [['a', 'b'], ['a', 'b']]
Notice that within the b
list the a
list is present twice - is the same object. So when I change the list a
, it is expected that in b
what I entered appears in both elements. No surprise there.
In your code, you should be doing this, either directly or indirectly. But in the snippet you pasted above, this does not happen - you recreate the movi_anexos
list as a new object, for each iteration of your first for
, for example.
Python has a syntax that lets you create a copy of a list by referring to it that might help you:
When you insert a list as an item from another, instead of just the variable name, put a slice from the beginning to the end of the list you are inserting (ask the elements [:]
a slice omitting them initial and final elements implies a slice from beginning to end). List slices are copies of the original list, and a copy of the beginning to the end is nothing less than a copy of the list.
So if you write code like:]
movimentacoes.append([data, texto, movi_anexos[:])
should prevent the same list from being present in more than one position in its data structures. (but I emphasize that in the section that is here this would not happen - but I do not know for example if your data
and texto
are lists too - if they are, give them the same treatment)
And finally, unrelated to your doubt: the way you're trying to organize this data seems very problematic - you'll get a% of lists within lists, with no defined length, no defined depth - looks like worst nightmare to be able to recover anything later.
Remember that there are also dictionaries, and above all, you can create your own classes that will manage your information very well structured and can help you to work with the data you have.