Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming items in python list [duplicate]

I have a python list as below:

['item1','item2','item3'] 

I am trying to rename the items in the list as

['person1','person2','person3']

Could anyone guide me. Thanks

like image 792
scott martin Avatar asked Aug 26 '26 23:08

scott martin


1 Answers

You can use replace to change "item" to "person", and you can use a list comprehension to generate a new list.

items = ['item1','item2','item3']
people = [item.replace('item', 'person') for item in items]

Result:

['person1', 'person2', 'person3']
like image 102
khelwood Avatar answered Aug 28 '26 12:08

khelwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!