Reputation: 3
I am trying to append a data in to names_list i get it that names_list = list but i don't know why names_list[i] become a string and how do i convert it back to list?
for data in f:
drawingdata = data.split()
names = drawingdata[0]
heights = drawingdata[1]
names_list = names.strip('][').split(',')
for i in range(len(new_name)):
names_list[i].append(new_name[i])
Upvotes: 0
Views: 728
Reputation: 5458
i don't know why names_list[i] become a string and how do i convert it back to list?
names_list
is a list
names_list[i]
is i_th element of the list (0-indexed), that's why it's a string
What you want to do is names_list.append
, NOT names_list[i].append
(you want to append to the whole list, not to an element of the list)
Also, if new_name
is a single name, then the whole loop is unnecessary because you will append each letter of the name as a separate entry!
Just do names_list.append(new_name)
.
Upvotes: 1
Reputation: 344
I think this is the reason:
names_list[i]
returns a string because drawingdata
is a list with each value a string, and names
is the first item in drawingdata
so it is a string. Therefore names_list
is splitting names
into an list with strings, so names_list[i]
is a string.
To answer your question about converting name_list[i]
into a list, you can try [names_list[i]]
, which will give you a list with only names_list[i]
in it.
Upvotes: 0