0

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"']
agf
178k45 gold badges300 silver badges241 bronze badges
asked Mar 18, 2012 at 0:55
1
  • 4
    It looks like you're starting with a list, but that list contains a single string that contains commas and quotes. Is that really what you want? Or do you want a list with four items, an integer and three strings? Commented Mar 18, 2012 at 0:58

3 Answers 3

1

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.

answered Mar 18, 2012 at 1:03
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Mar 18, 2012 at 1:00

Comments

0

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

answered Mar 18, 2012 at 1:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.