pythonNoob123
pythonNoob123

Reputation: 1

python list inquiry

My list looks like this:

[',100,"","Rock outcrop","Miscellaneous area"']

I want to add in double quotes before the 100.

so that it looks like this:

['"",100,"","Rock outcrop","Miscellaneous area"']

I tried the insert function, but that only adds something to my list before the start of my list. When I did insert, it looked like this

['', ',100,"","Rock outcrop","Miscellaneous area"']

Upvotes: 0

Views: 167

Answers (3)

Jay
Jay

Reputation: 1003

Suppose you have the list

lst = [',100,"","Rock outcrop","Miscellaneous area"']

So what you can do is get the first element in the list using lst[0] and then changing it and assigning it back to lst[0], so

lst[0] = '""' + lst[0] 

would do it

Edit: the problem you seem to be having is that you are making an array of one element that is a string. So you have a list with the element

',100,"","Rock outcrop","Miscellaneous area"'

which is a string what you probably want instead is to do something like

lst = [100,"","Rock outcrop","Miscellaneous area"]

and then do the insert

Upvotes: 0

agf
agf

Reputation: 176750

What you've got is a single string, containing

,100,"","Rock outcrop","Miscellaneous area"

inside a list. If what you want is to add "" to the beginning of that string, then you can do that by doing

mylist[0] = '""' + mylist[0]

But I assume that you probably want an actual sequence of strings, in which case you want

import ast
mylist = ast.literal_eval('""' + mylist[0])
#mylist is now ('', 100, '', 'Rock outcrop', 'Miscellaneous area')

ast.literal_eval interprets a string as a Python literal, in this case a tuple.

Upvotes: 1

Keith
Keith

Reputation: 43024

What you actually have is a list of one string. So just pull out the string.

s = l[0]
s = '""' + s

But... that's an odd use of a list and string. You might want to use a different structure.

Upvotes: 0

Related Questions