append/insert a value/string to a list element dynamically in Python?

Gaurav Jain
my_list = ['cat','cow','dog','rabbit']

but what I want is to append/insert a character(or string) to one or more element(not all).

something like

my_list = ['cat','cow_%s','dog_%s','rabbit']%('gives milk','bark')

now updated list should look like

my_list = ['cat','cow_gives milk','dog_bark','rabbit']

one way is to do this is manually change/update the element one by one e.g my_list[2]=my_list[2]+"bark"

but I don't want that because my_list is long(around 100s element ) and 40+ need to be changed dynamically.

In my case It is like

my_list = ['cat','cow_%s','dog_%s','rabbit']
for a in xyz:   #a is a string and xyz is a list of string 
      my_list=my_list%(a,a+'b')
      fun(my_list)
jonrsharpe

You could do something like this:

changes = {"cow":"gives milk", "dog":"bark"}

my_list = [item if item not in changes else "_".join([item, changes[item]]) 
           for item in my_list]

For greater efficiency if you will do this repeatedly, as JAB suggests in the comments, build a dictionary to map items to their locations (indices) in the list:

locations = dict((s, i) for i, s in enumerate(my_list))

You can then use this to find the corresponding items.


If you are stuck with the list of strings, some ending "%s", and list of things to put in them, I guess you will have to do something like:

for i, s in enumerate(my_list): # work through items in list with index
    if s.endswith("%s"): # find an item to update
        my_list[i] = s % xyz.pop(0) # update with first item popped from xyz

Note that xyz will have to be a list for this, not tuple, as you can't pop items from a tuple.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing dynamically an element from a list

From Dev

Add list elements dynamically to ul element

From Dev

Dynamically load JSON on click and populate list element

From Dev

Dynamically add new element to specific list

From Dev

Edit an element in a list - python

From Dev

python replace an element in a list

From Dev

Insert an element into a list in python

From Dev

Validating list element in Python

From Dev

Replacement of element in a list of python

From Dev

Update element in the list in Python

From Dev

Python: pair a single element with every element in a list

From Dev

Python: Appending an element to an element of a list of lists

From Dev

how to get the index of dynamically created element (list item)?

From Dev

C# WPF dynamically create a GUI element for each object in list

From Dev

How to dynamically add element with tabs in the list. Angular js

From Dev

Dynamically move a div above a list element using Javascript

From Dev

how to know the element that performed the click in a checkbox list dynamically added (javascript)

From Dev

Python: first element in a nested list

From Dev

Python: Change an element in a list of lists

From Dev

Python zip single list element

From Dev

modifying list element in Python dictionary

From Dev

Python: duplicating each element in a list

From Dev

Parse xml element into list in Python

From Dev

Python append to an string element in a list

From Dev

Python - If any element in list is in line

From Dev

Remove adjacent element in a list in python

From Dev

Python split list by one element

From Dev

removing element from a list in python

From Dev

Remove element from list in Python

Related Related

HotTag

Archive