Python Forum
Compare Two Lists and Replace Items In a List by Index
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare Two Lists and Replace Items In a List by Index
#1
I'm working on a problem in which I'd like to compare two lists of numbers:

list_a = [0, 1, 2, 3]
list_b = [4, -2, 3, -1]
Then, I'd like to create a new list_c that transforms the list_a by examining every negative number in list_b and then replaces the corresponding number in list_a (at the same index number when compared to list_b) with None.

list_c = [0, None, 2, None]
I welcome your suggestions on what would be the most efficient way to achieve this. Thank you.
Reply


Messages In This Thread
Compare Two Lists and Replace Items In a List by Index - by nagymusic - May-09-2020, 10:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  removing one list element without using its index paul18fr 7 1,748 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
Sad The issue with compare 2D lists przonak007 18 3,930 Jul-17-2024, 07:31 AM
Last Post: Pedroski55
  Compare lists w_i_k_i_d 6 1,897 May-23-2024, 07:23 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 3,185 May-22-2023, 10:39 PM
Last Post: ICanIBB
Thumbs Down I hate "List index out of range" Melen 20 10,957 May-14-2023, 06:43 AM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,893 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 1,622 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 2,120 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 2,126 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 3,386 Oct-01-2022, 07:15 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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