Python Forum
Changing elements of a list to match another list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing elements of a list to match another list
#2
If I correctly understand the objective then no iteration is needed.

You just want to replace first element in every row in matrix with value which in reference list. As indexes of rows in matrixes and elements in reference list are the same one can just:

>>> lst = [[2,13,22,40],[8,13,22,40],[24,13,22,40]]                  
>>> reference = [5, 10, 30]                                          
>>> for i, v in enumerate(reference): 
...     lst[i][0] = v 
...
>>> lst                                                              
[[5, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]
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


Messages In This Thread
RE: Changing elements of a list to match another list - by perfringo - Jul-11-2019, 01:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 2 258 May-01-2024, 07:16 AM
Last Post: Gribouillis
  unable to remove all elements from list based on a condition sg_python 3 492 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 516 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,251 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,599 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 952 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 1,155 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,903 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,050 Aug-24-2022, 05:26 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,620 Aug-06-2022, 11:39 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020