2

I have a list of strings.

If one of the strings (e.g. at index 5) is the empty string, I want to replace it with "N".

How do I do that? The naive method (for a java programmer) does not work:

string_list[5] = "N"

gives

'str' object does not support item assignment

(string_list originally came from a .csv-file- that is why it might contain empty strings.)

4
  • Looks like string_list is actually a string, not a list of strings. Lists are mutable, so you should be able to replace its item just like you did. Commented Aug 23, 2012 at 8:40
  • You have simple string not list, and you can use replace method. Commented Aug 23, 2012 at 8:40
  • 1
    could you post an example of your string_list ? Commented Aug 23, 2012 at 8:43
  • I need to look at my code again and explain better. Give me a sec. Commented Aug 23, 2012 at 8:47

5 Answers 5

5

Your error seems to indicate that your string_list is not a list of string but a real string (that doesn't support assignement because a string is immutable).

If your string_list was a real list of strings, like this for example : string_list = ["first", "second", "", "fourth"], then you will be able to do string_list[2] = "third" to obtain string_list = ["first", "second", "third", "fourth"].

If you need to automatically detect where an empty string is located in your list, try with index :

string_list[string_list.index("")] = "replacement string"

print string_list
>>> ["first", "second", "replacement string", "fourth"]
Sign up to request clarification or add additional context in comments.

1 Comment

I made a dumdum mistake: when splitting my original line I gave it an index so a string was returned, not a list. Feeling embarrased
2

You say you have a list of strings but from you error it looks like you're trying to index a string

l =  ["a", "b", "", "c"]

Is a list of strings.

l = ["N" if not x else x for x in l]

Is a list of strings with empty strings replaced with "N"

2 Comments

Nice. Good something worthwhile came out of my mistake.
Anything for someone with a name like that. Made me laugh :)
1

Try this:

>>> s = 'abc de'
>>> s.replace(' ', 'N')
'abcNde'

As mentioned by the others, it sounds like string_list is actually a string itself, meaning that you can't use assignment to change a character.

1 Comment

@Aesthete Ha, no worries - I did too at first.
0

In python work with lists and convert them to strings when need be.

>> str = list("Hellp")

>> str
['H', 'e', 'l', 'l', 'p']

>> str[4] = 'o'

>> str
['H', 'e', 'l', 'l', 'o']

TO make it a string use

>> "".join(str)

'Hello World'

Python strings are immutable and they cannot be modified.

Or you could use List Comprehensions.

Some thing like:

>> myStr = ['how', 'are', 'yew', '?']

>> myStr = [w.replace('yew', 'you') for w in myStr]

Comments

0

The following example iterates through lists of lists (sublists), in order to replace a string, a word:

myoldlist=[['aa bbbbb'],['dd myword'],['aa myword']]
mynewlist=[]
for i in xrange(0,3,1):
    mynewlist.append([x.replace('myword', 'new_word') for x in myoldlist[i]])

print mynewlist
# ['aa bbbbb'],['dd new_word'],['aa new_word']

1 Comment

Your two answers seem to be almost identical you probably could consider refer one as your answer while the other you simply need to post a link in the comment (once your rep is >= 50)

Your Answer

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.