Replacing elements in a list of lists, with elements from another list

python list replace element at index
python replace element in list with another element
replace element in list python
python replace multiple items in list
python lists replace element
replace one list with another python
replace items of a list python
python change element from list

I have two lists:

abc = [[1, 11, 111, 111], [2, 22, 222, 2222], [3, 33, 333, 3333]]
bbb = [12, 13, 34]

I want to replace the 2nd element from each sublist of list abc with element from bbb, so I can have a list that looks like this:

[[1, 12, 111, 111], [2, 13, 222, 2222], [3, 34, 333, 3333]]

I know I have to use list comprehension, but I just can't figure it out.

The best I can think of is:

newlist = [i[1]=bbb for i in abc]

You could do something like this, using zip:

abc = [[1, 11, 111, 111], [2, 22, 222, 2222], [3, 33, 333, 3333]]
bbb = [12, 13, 34]

result = [f[:1] + [s] + f[2:] for f, s in zip(abc, bbb)]

print(result)

Output

[[1, 12, 111, 111], [2, 13, 222, 2222], [3, 34, 333, 3333]]

Python: Replacing an element in a list of lists (#2), You are having four references to same object by * 4, use instead list comprehension with range for counting: my_nested_list = [[0,0] for count  Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list. Method #1: Using Iteration filter_none

You can use zip:

abc = [[1, 11, 111, 111], [2, 22, 222, 2222], [3, 33, 333, 3333]]
bbb = [12, 13, 34]
result = [[a, b, *c] for [a, _, *c], b in zip(abc, bbb)]

Output:

[[1, 12, 111, 111], [2, 13, 222, 2222], [3, 34, 333, 3333]]

Python, Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list. I have two lists: abc = [[1, 11, 111, 111], [2, 22, 222, 2222], [3, 33, 333, 3333]] bbb = [12, 13, 34] I want to replace the 2nd element from each sublist of list abc with element from bbb, so I

This question has already been answered, but as an alternative, you could think about using numpy, especially if your arrays are large:

import numpy as np
abc = [[1, 11, 111, 111], [2, 22, 222, 2222], [3, 33, 333, 3333]]
bbb = [12, 13, 34]

abc = np.array(abc)
abc[:,1] = bbb

>>> abc
array([[   1,   12,  111,  111],
       [   2,   13,  222, 2222],
       [   3,   34,  333, 3333]])

# You can convert back to list too if so desired:
>>> abc.tolist()
[[1, 12, 111, 111], [2, 13, 222, 2222], [3, 34, 333, 3333]]

How to replace existing element in ArrayList, How do you replace an item in a list Python? Replacing List Items There is a list of integers. Replace negative by -1, positive - by number 1, keep zero unchanged. 1st option - filling a new list depending on the values of the first one :

Python list, How do you find the index of an element in a list Python? Approach #2 : Pythonic Naive This is another naive approach, but more pythonic.For every inner list, it returns the i th position (which is its ordinal number) and then multiplies it with the length of that particular inner list in order to return the desired output.

Python list: how can I replace certain items of the list?, If you want to replace the existing value with another o So it is permitted and easy to change elements in a list. Let's make More on replacing items on lists. Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a Python program to check if all items of a list is equal to a given string. Next: Write a Python program to check if the n-th element exists in a given list.

Iterate over large list of lists and replace its elements, You're looping in incorrect order, take the advantage of the fact that dictionary provides O(1) lookups. So, you should loop over the list and  The elements of the list can contain a string, integers, and objects. You can update these items of the list with the desired position or location using the index. If you don’t know the index of the element, you can perform a loop to compare and change or update the element of the list.

Comments
  • Thank you. This is what I'm using. Easy to understand.
  • This is also correct, but I don't quite understand the syntax.