list=[]
list_start=[[1,2], [2,2], [1,3]]
    
for element in list_start:
        list.append(element)

list[0].append([2,3])

print "list=", list

The result is,
list= [[1, 2, [2, 3]], [2, 2], [1, 3]]
instead of
list= [[1, 2], [2, 3]], [2, 2], [1, 3]]

If check:
>>> list[0]
[1, 2, [2, 3]]
>>> list[0][0]
1
>>> list[0][2]
[2, 3]

Why does it like this? I am confused. Thanks a lot!

Recommended Answers

All 11 Replies

Use .extend instead of .append

Thanks for the help! When I use .extend, it becomes this
list= [[1, 2, 2, 3], [2, 2], [1, 3]]
instead of
list= [[[1, 2], [2, 3]], [2, 2], [1, 3]]

Any suggestion? I am confused about this question, thanks a lot!

prepare the lists first element and extend the list from copy or start_list. Never use list as variable name as it is built in type:

list_start=[[1,2], [2,2], [1,3]]

# produce new list with tail ([1:]) of list and as first element the old first one and list [2,3]
# new head ([0])
new_list = [[list_start[0], [2, 3]]]
new_list.extend(list_start[1:])

print "new list=", new_list

Take as look at list methods here. You can use insert

list_start=[[1,2], [2,2], [1,3]]
list_start.insert(1, [2,3])
print list.start

I appreciate, the example can be solved by this way for it is the frist element in the list. However, in the real world, it is in the loop and I need to append an element once a time into different sublists on different conditions, e.g. into list_start[1] this time, and list_start[2] the next time. So it will be hard to use this stratergy, right? Or I miss something here?

Oh, thanks for reminding me the "list" usage, I did the example in a hurry but I don't think about that.

woooee,
insert is similiar to append in almost all the ways. I need to put [2,3] in one of the subgroups and get something like [[1, 2], [2, 3]], [[2, 2],[3,4]], [1, 3]] instead of [[1, 2, [2, 3]], [2, 2, [3,4]], [1, 3]].
Tony's solution will work for the first element, but hard to do it in loop and differnt subgroups. Thanks a lot.

Looks that maybe you better use dictionary with integer hash if you are free to do so? You only need to have long enough list prepared, and you can use insert to anywhere easily by the insert method, like woooee sayed. If you would say clearly the use cases, we could help you better. Especially the real purpose of these lists, so we could get the choice of data structure right from beginning. Now I have not Idea why and what you want to do exactly and why, you can do your second list (you built copy of list manually in your first code, remember?

you can just assign to indexed positions, instead of insert also

list_start=[[1,2], [2,2], [1,3]]

# deepening the sublist one level for every insert
list_start[0] = [list_start[0][:],[2,3]]    # slice to make copy
print list_start
list_start[1] = [list_start[1][:],[[5,6]]] # slice to make copy
print list_start
list_start[2] = [list_start[2][:],[7,8]] 
print list_start

I think you have badly chosen situation for start, you should have constang 3 level list:

# constant three level list
list_start=[[[1,2]], [[2,2]], [[1,3]]]

list_start[0].append([2,3])
print list_start
list_start[1].append([5,6])
print list_start
list_start[2].append([7,8])
print list_start

Man! How can you know it?? I always thought I initialized right, I can see now it was not. I like your assignment solution too, I tried but failed. Nice code, thanks a lot!

It is sometimes tough concept to put one element list instead of not sequence value. Even the physisist, consider scalar and vector of one the same.

This is for biological stuff, algorithm is OK, implementation is hard!! But it is eye-opening, nice to learn from you guys from computer field. Thank a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.