Bottom Page

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
Quote
#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.
Quote
#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.
Quote
#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]
Quote
#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']
Malt and newbieAuggie2019 like this post
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Code Editing Help Rememberthecreed 2 185 Feb-08-2020, 09:56 AM
Last Post: Rememberthecreed
  Operations on indexed variables in loop Fibulavie 1 220 Aug-14-2019, 06:07 AM
Last Post: fishhook
  Editing Dictionaries Astrikor 7 971 Aug-15-2018, 05:05 PM
Last Post: Astrikor
  If you deque a list, can it still be indexed? netrate 6 3,114 Nov-07-2016, 12:19 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)