Hjouji Anas Hjouji Anas -5 years ago 117
Python Question

Python - using list() and manipulating lists

I seem to have a problem with my list generating script. I'm trying to convert a string into a list of string-lists, in order to use it in a specific fonction.

For exemple I want to convert the string

to a :
so the script i wrote was:

s='some string'
for i in range(len(s)):
t[i][0] = list(s)[i]
return t

the problem is that this returns
instead of

I found another way to achieve that, but i can't seem to find the problem of this script.
So,if anyone could help me, i would really appreciate it. Thanks in advance.

Answer Source

This is a classic mistake: [[0]]*n creates a list of n references to a unique object [0]. Use this instead to create a list of n different objects all equal to [0]: [[0] for i in range(n)]. This way, you can then change the value of each of them without affecting the others.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download