Using .replace to change words in a list

1208
9
Jump to solution
06-30-2017 11:07 AM
AhmadSALEH1
Occasional Contributor III

Hi,

I am trying to with a script that replaces the word "feet" to "meter" in a list:

AreaList = ["1000 feet", "23908 feet", "1200000000 feet"]
for area in list(AreaList):
       area.replace("feet", "meters")

print AreaList

but I still see the results as the following:

the word feet still in the list! what might be my mistake ?

Thanks

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

try this: 

AreaList = ["1000 feet", "23908 feet", "1200000000 feet"]

MeterList = [a.replace("feet","meters") for a in AreaList]

print MeterList

View solution in original post

0 Kudos
9 Replies
MitchHolley1
MVP Regular Contributor

try this: 

AreaList = ["1000 feet", "23908 feet", "1200000000 feet"]

MeterList = [a.replace("feet","meters") for a in AreaList]

print MeterList
0 Kudos
AhmadSALEH1
Occasional Contributor III

Thanks Mitch this worked fine with me, also I figure out where the error was in my code, it should be written as this:

AreaList = ["1000 feet", "23908 feet", "1200000000 feet"]

for area in list(AreaList):

    area=area.replace("feet", "meters")

    print area

by the way I cannot see "mark as correct answer" button next to your name, this is the second time that happens with me!!!

DanPatterson_Retired
MVP Esteemed Contributor

Ahmad... that is because you often open up 'Discussions' rather than 'Questions' ... Discussions are just that, something that you want to talk about, the question option is the only one that allows you to mark a 'Correct Answer' or words to that effect.

0 Kudos
curtvprice
MVP Esteemed Contributor

I'm a moderator so I converted this thread to a question and marked Mitch's answer correct.

DanPatterson_Retired
MVP Esteemed Contributor

Curtis... me too... you should link your comment it to Ahmad's thread rather than mine.  Hopefully that is what he wanted? wouldn't hurt but I think he also needs the link that shows the differences between discussions and questions.

0 Kudos
RandyBurton
MVP Regular Contributor

See List Comprehensions for more information on Mitch's answer.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

You printed AreaList, which doesn't get changed even though you would be printing and changing the values as you went.

>>> AreaList = ["1000 feet", "23908 feet", "1200000000 feet"]
>>> a = [i.replace('feet','meters') for i in AreaList]
>>> a
['1000 meters', '23908 meters', '1200000000 meters']
>>> 

Now see what went wrong.

>>> for i in AreaList:
...      i = i.replace('feet', 'meters')  # note the reassignment
...      print("i value {}".format(i))
...      
i value 1000 meters
i value 23908 meters
i value 1200000000 meters

>>> print("Area list at the end \n{}".format(AreaList))
Area list at the end 
['1000 feet', '23908 feet', '1200000000 feet']
>>> 

Got to watch what you think is going on... just because a value is pulled from and list and changed, doesn't mean the value in the original list gets changed.  You have to recollect the changes as I did in the original example, or replace them in the input list.

ClintonDow1
Occasional Contributor II

The string.replace() function does not change the string in place, it returns a new string with the replaced words.

Instead you could do:

feet_list = ["1000 feet", "23908 feet", "1200000000 feet"]
meter_list = []
for area in feet_list:
    s = area.replace("feet", "meters")
    meter_list.append(s)
print(meter_list)
# ['1000 meters', '23908 meters', '1200000000 meters']

Or alternatively:

meter_list = [f.replace("feet", "meters") for f in feet_list]
print(meter_list)
#  ['1000 meters', '23908 meters', '1200000000 meters']
JoshuaBixby
MVP Esteemed Contributor

Ahmad SALEH, it is possible in Python to update list values in place, as opposed to constructing a new list, which is what a list comprehension does.  Looking at your original code, incorporating enumerate() allows you to keep your basic code structure and do the updates you want:

>>> AreaList = ["1000 feet", "23908 feet", "1200000000 feet"]
>>> for i, area in enumerate(AreaList):
...     AreaList[i] = area.replace("feet", "meters")
... 
>>> print AreaList
['1000 meters', '23908 meters', '1200000000 meters']
>>> 

For your specific situation, I think using a list comprehension makes more sense because you will be updating every list item.  In cases where the number of elements updated in a list is high, especially when nearly all elements are updated, constructing a new list and reassigning it back to your variable of choice often makes more sense then doing in-place updates.