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
#1
Hi everyone,

I have a list say suppose,
list= [Mysore,Bangalore,chennai,pune]

In this I want to edit the list like,
edited_list=[Mysore is a beautiful city,Bangalore,chennai,pune]

How can we do this,I tried different methods like insert,append,extend. But that didn't work.
Is there any python inbuilt module. Or how can we do this indexed position editing.

Thank you,
Shruthi LS
Reply
#2
Never use 'list' as name (this is built-in function).

What is the problem? Access items in list?

>>> lst = ['Mysore','Bangalore','chennai','pune']
>>> lst[0]
'Mysore'
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
#3
Ya thank you for the reply. But how can I edit by adding something to the zeroth position i.e., Mysore in this example. May it be anything.
Reply
#4
just use assignment
>>> my_list = ['foo', 'bar']
>>> my_list[0] = 'spam'
>>> my_list
['spam', 'bar']
>>> my_list = [1, 2, 3, 4]
>>> my_list[1:3] = [5, 6]
>>> my_list
[1, 5, 6, 4]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Operations on indexed variables in loop Fibulavie 1 1,929 Aug-14-2019, 06:07 AM
Last Post: fishhook
  If you deque a list, can it still be indexed? netrate 6 10,089 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