I have a list called l which contains the following elements:
['weight_Thomas', 'weight_Michael', 'weight_Donald', 'weight_Liam', 'weight_Ethan', .....]
I would like to change it to:
['Thomas', 'Michael', 'Donald', 'Liam', 'Ethan', .....]
so that only the names are in it without the string 'weight'.
CodePudding user response:
Try using map
with split
list(map(lambda x: x.split("_")[1], l))
CodePudding user response:
Another solution just using list comprehension:
values = ['weight_Thomas', 'weight_Michael', 'weight_Donald', 'weight_Liam', 'weight_Ethan']
value_new = [l.split('_')[1] for l in values]
Output:
['Thomas', 'Michael', 'Donald', 'Liam', 'Ethan']
CodePudding user response:
assuming your list is called items
names_only = [item.split("_")[1] for item in items]
CodePudding user response:
let tempArr = ['weight_Thomas', 'weight_Michael', 'weight_Donald', 'weight_Liam', 'weight_Ethan'];
let changedArr = tempArr.map(e=>e.split('_')[1]);
console.log(changedArr)
can you try this?