jbenfleming
jbenfleming

Reputation: 75

replacing string in list of lists python

I have a list containing three separate lists:

James =     ['Alvin Kamara','Alex Collins','Michael Thomas',
          'Adam Thielen','Evan Engram','Lamar Miller']

Ben =   ['Todd Gurley II','Royce Freeman','Larry Fitzgerald',
        'Cooper Kupp','Benjamin Watson','Robby Anderson']

Chris =     ['Dion Lewis','Rex Burkhead','Julio Jones',
          'Keenan Allen','Zach Ertz','Demaryius Thomas']

I want to remove the text ' II', which if you look in the "Ben" list, would change 'Toddy Gurley II' to 'Todd Gurley'. My current attempt is this:

master_list = [James,Ben,Chris]

for owner in master_list:
    owner = [word.replace(' II','') for word in owner]

print(master_list[1])

But the output I get is:

['Todd Gurley II', 'Royce Freeman', 'Larry Fitzgerald', 'Cooper Kupp', 'Benjamin Watson', 'Robby Anderson']

Upvotes: 1

Views: 8066

Answers (4)

jstelman
jstelman

Reputation: 36

In your outer for loop, you update a copy of the owner variable, but do not replace it in the list. You can instead add the updated owner variable to a new list:

new_list = []
for owner in master_list:
    owner = [word.replace(' II','') for word in owner]
    new_list.append(owner)

print(new_list[1])

['Todd Gurley', 'Royce Freeman', 'Larry Fitzgerald', 'Cooper Kupp', 'Benjamin Watson', 'Robby Anderson']

Alternatively, you can use enumerate() over the owner list, in order to update the original master_list in place:

for i, owner in enumerate(master_list):
    owner = [word.replace(' II','') for word in owner]
    master_list[i] = owner

print(master_list[1])

['Todd Gurley', 'Royce Freeman', 'Larry Fitzgerald', 'Cooper Kupp', 'Benjamin Watson', 'Robby Anderson']

Upvotes: 1

vash_the_stampede
vash_the_stampede

Reputation: 4606

We can combine enumerate and list comprehension here and evaluate using the [-2:] position of our strings

l = [James, Ben, Chris]

for idx, item in enumerate(l):
    l[idx] = [i[:-3] if 'II' in i[-2:] else i for i in item]
chrx@chrx:~/python/stackoverflow/9.22$ python3.7 uk.py
[['Alvin Kamara', 'Alex Collins', 'Michael Thomas', 'Adam Thielen', 'Evan 
Engram', 'Lamar Miller'], ['Todd Gurley', 'Royce
Freeman', 'Larry Fitzgerald', 'Cooper Kupp', 'Benjamin Watson', 'Robby
Anderson'], ['Dion Lewis', 'Rex Burkhead', 'Julio Jones', 'Keenan
Allen', 'Zach Ertz', 'Demaryius Thomas']]

Upvotes: 1

brno32
brno32

Reputation: 424

when you call owner = [word.replace(' II','') for word in owner], you aren't updating master_list; you're modifying a copy of one of its elements.

You can either loop through and update master_list by making calls like

master_list[1] = [word.replace(' II','') for word in owner]

Or you could reference a new array:

new_master_list = []
for owner in master_list:
    new_master_list.append([word.replace(' II', '') for word in owner])

print(new_master_list[1])

Upvotes: 1

Ajax1234
Ajax1234

Reputation: 71471

You can use re:

import re
print([re.sub('\sII$', '', i) for i in master_list[1]])

Output:

['Todd Gurley', 'Royce Freeman', 'Larry Fitzgerald', 'Cooper Kupp', 'Benjamin Watson', 'Robby Anderson']

Upvotes: 2

Related Questions