Replace string in specific index in list of lists python
NickName:Rief Ask DateTime:2020-03-02T11:39:54

Replace string in specific index in list of lists python

How can i replace a string in list of lists in python but i want to apply the changes only to the specific index and not affecting the other index, here some example:

mylist = [["test_one", "test_two"], ["test_one", "test_two"]]

i want to change the word "test" to "my" so the result would be only affecting the second index:

mylist = [["test_one", "my_two"], ["test_one", "my_two"]]

I can figure out how to change both of list but i can't figure out what I'm supposed to do if only change one specific index.

Copyright Notice:Content Author:「Rief」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60482258/replace-string-in-specific-index-in-list-of-lists-python

Answers
Chris 2020-03-02T03:43:00

Use indexing:\n\nnewlist = []\nfor l in mylist:\n l[1] = l[1].replace(\"test\", \"my\")\n newlist.append(l)\nprint(newlist)\n\n\nOr oneliner if you always have two elements in the sublist:\n\nnewlist = [[i, j.replace(\"test\", \"my\")] for i, j in mylist]\nprint(newlist)\n\n\nOutput:\n\n[['test_one', 'my_two'], ['test_one', 'my_two']]\n",


More about “Replace string in specific index in list of lists python” related questions

Replace string in specific index in list of lists python

How can i replace a string in list of lists in python but i want to apply the changes only to the specific index and not affecting the other index, here some example: mylist = [["test_one", "test_...

Show Detail

Replace a string in a specific index in a line using python

I'm working on a python project and I want to replace a specific word in a line in a file (text.txt) with another string. Like I have this line <string name="AppName">old string</

Show Detail

Replace a string in list of lists

I have a list of lists of strings like: example = [["string 1", "a\r\ntest string:"],["string 1", "test 2: another\r\ntest string"]] I'd like to replace the "\r\n" with a space (and

Show Detail

Python find index of list of list, but only at specific index of internal list

I have a list of lists in the format: list = [["5:2", "1:0"],["3:4", "5:2"],["1:2", "1:1"],["4:5", "1:0"]]&

Show Detail

Returning index of list in list of lists that contains a specific string

I think I'm close with this function but I'm having trouble. I need to return the index of the list in a list of lists that contains a particular string. For example, if the list of lists was [['.'...

Show Detail

Remove a specific index from a list while iterating in Python

I learn how to remove items from a list while iterating from here by: somelist = [x for x in somelist if determine(x)] Further, how do I remove a specific index from a list while iterating? For

Show Detail

Python create list from specific indexes in a list of lists

I have a list of lists and I want to create multiple smaller lists from specific indexes in this list. If I have this big "matrix" can I create a small list made up of integers collected from this ...

Show Detail

python replace specific elements in a string

I have a string '1472_1 2014-6-19' and I want to replace whatever number is after the underscore(in this case number one) with the word 'Normal', what I did was to find the index of the element tha...

Show Detail

Get specific index from map with list of lists as value

I cant figure how to do the following in java In groovy, if I want to iterate over a map and inside that map the value is a list of lists. then get a specific index from a list of lists i.e. the

Show Detail

Using replace() with for loop for nested lists in Python

I am working with a set of data that is a list of lists. I am trying to replace a part of an element in the nested lists; namely, there are quotes around some of the elements in these lists that I...

Show Detail