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.)

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156

5 Answers5

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"]
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • 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* – The Unfun Cat Aug 23 '12 at 08:51
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"

Aesthete
  • 18,622
  • 6
  • 36
  • 45
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.

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
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]
Next Door Engineer
  • 2,818
  • 4
  • 20
  • 33
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']
Jamal
  • 763
  • 7
  • 22
  • 32
Estatistics
  • 874
  • 9
  • 24
  • 2
    Your two answers seem to be [almost identical](http://stackoverflow.com/questions/13781828/replace-a-string-in-list-of-lists/36938886#36938886) 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) – Ian Apr 29 '16 at 13:18