Replacing data in a list with sublists

Good morning,
I need to replace the first item in a list (fourth) consisting of four sublists.

For example, I replace 

<VCItem ID="1">
in the first item of the first sublist

<VCItem ID="2">
in the first item of the second sublist

and so on.

Basically, it puts

<VCItem ID="4">
in the first item of all the sublists.

Any help?
Thanks

Something like…
count = 1
for subList in bigList:
subList[0] = substitute[count]
count += 1

Or if you wanna be more fancy

for i, sub_list in enumerate(big_list, start=1):
sub_list[0] = substitute[i]

Can you explain how this is different from your other topic?
List sublist - Dynamo

If the original thread hasn’t led to an acceptable solution yet, then you should continue the conversation there rather than starting a new thread.

As for your current situation… mez is using append, which is correctly adding each increment to the list. quart on the other hand is being set to the new value, so you’re overwriting it every time and then only returning the final iteration.

Thanks for the reply.

I tried the previous method, but I still get an error (see image).

First, I create the quart list... made up of 4 sub-quart lists.

Then I want to insert the new numbers by incrementing the first entry in the quart list.

I'm expecting

<VCItem ID="1">
<VCItem ID="2">
<VCItem ID="3">
<VCItem ID="4">

It didn't work, so I tried changing the program.

It only inserts
<VCItem ID="4">

You’re still not appending to a list. You’re just replacing the value in quart each time. You’re effectively doing this:

quart = 1
quart = 2
quart = 3
quart = 4
return quart (quart = 4)

You’re overriding the value each time in the loop and then asking for the final value. You want to append that value (or the entire sublist in this case) each time you update the value, because appending will add to a new list rather than replace an existing one.

I also don’t think you’re creating your sublist properly (quart=quart+quart). Each loop is duplicating the previous sublist. You’re effectively doubling the list length with each iteration.

quart = [["A","B","C","D"]]
for rr in range (0,4):
    quart = quart+quart
    print("Loop:",rr)
    print("quart:",quart)

Again, you marked your previous thread as Solved and this seems to be the same issue (or a very similar one). If this is a different issue, then you need to clearly indicate why it’s different. If it’s not, then you need to continue the original thread rather than rehashing the same problem in a new topic.

Thanks for your reply.

Unfortunately, I don't understand... please bear with me.

The list is:

quart=quarta+quart

quarta list # quart list

quarta is sent 4 times in quart.

I get quart with 4 identical sublists.
(Image #1)

Then I run on quart
what you told me in the post

"List sublist - Dynamo" (Image #3)

I get image #4 with the same repeated data.

<VCItem ID="4">

I don't understand the error.


Sorry,
but some sentences are incomplete.

The # symbol
obscures the rest.

In particular:

List quarta is different from List quart

I missed the difference between quart and quarta. Adding nothing (an empty list) is a really strange way to duplicate a list. If you want quart to be a list of 4x quarta, you really should append. It’s also the reason your values are all the same.

for rr in range(0,num): 
    quart.append(quarta)

It seems like all these issues stem from a misunderstanding of how to handle cumulative list structures. The method you’re using with quarta+quart would be joining those lists together, rather than replicating them x number of times. Because you’re replacing quart with itself, you end up with a list of 4 quarts. They are the same object, so when you replace one you’re actually replacing them all.

Here you can see I only replace the first item of the first sublist, but all sublists show the new value. That’s because each sublist is quart and I replaced the index of quart.

What do your inputs (quarta and quart) look like and what output are you wanting? Without all those pieces we’re only guessing.

Here’s the difference between joining lists and appending.

I created the quart list as shown:

quart.append(quarta)

but the problem remains.

It's strange because the quart list is then processed with the loop that works... used for other similar things.

You’re still appending an object, quarta. When you later update quarta, you’re updating the definition of that object and all instances that exist in that list. You need to build your combined list without reusing specified objects.

All of this is already handled in the solution to the original thread you opened. Until you can explain why this is a different issue warranting a new conversation, I’ll be locking this thread from further discussion. You are more than welcome to remove the solution from the original thread if it’s no longer working for you or if you just need additional explanation for what it’s doing.