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
#1
I am trying to iterate through a list of list and increase the first element in each sublist until it matches an integer on another list...

list1=[[2,13,22,40],[8,13,22,40],[24,13,22,40]]
reference_list=[5, 10, 30]
for i in list1: 
    while i[0]!=i in reference_list:        
        i[0]=i[0]+1
print(list1)
Right now my output is:

[[2, 13, 22, 40], [8, 13, 22, 40], [24, 13, 22, 40]]
(Nothing is getting modified) My desired output is:

[[5, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]
(so that the first integer is increased until it matches an integer on the reference_list)

I have dabbled using the set function but I can't seem to get that to work.

Any ideas are appreciated!
Reply


Messages In This Thread
Changing elements of a list to match another list - by rockproper - Jul-11-2019, 12:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 451 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 484 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,175 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,547 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 922 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Checking if a string contains all or any elements of a list k1llcod3 1 1,109 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,851 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 2,006 Aug-24-2022, 05:26 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,570 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,157 May-17-2022, 11:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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