1

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']

4 Answers 4

2

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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']

Comments

1

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

Comments

1

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']]

Comments

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.