Python Forum
Indexed position editing of a lst
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indexed position editing of a lst
#5
If its about string replacement then there can be different scenarios:

(1) Replace one item in list (use assignment as suggested by buran):

>>> foods = ['ham', 'spam']
>>> foods[0] = ' '.join([foods[0], 'and eggs'])
>>> foods
['ham and eggs', 'spam']
(2) Replace all values, same text:

>>> foods = ['ham', 'spam']
>>> foods = [' '.join([food, 'and eggs']) for food in foods]
>>> foods
['ham and eggs', 'spam and eggs']
(3) Replace all values, different text:

>>> foods = ['ham', 'spam']
>>> add_ons = ['and eggs', 'and spam']
>>> foods = [' '.join(pair) for pair in zip(foods, add_ons)]
>>> foods
['ham and eggs', 'spam and spam']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Indexed position editing of a lst - by ShruthiLS - Sep-26-2019, 07:13 AM
RE: Indexed position editing of a lst - by buran - Sep-26-2019, 07:47 AM
RE: Indexed position editing of a lst - by perfringo - Sep-26-2019, 09:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Operations on indexed variables in loop Fibulavie 1 1,941 Aug-14-2019, 06:07 AM
Last Post: fishhook
  If you deque a list, can it still be indexed? netrate 6 10,120 Nov-07-2016, 12:19 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020