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
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 179 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,321 May-22-2023, 10:39 PM
Last Post: ICanIBB
Thumbs Down I hate "List index out of range" Melen 20 3,304 May-14-2023, 06:43 AM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 914 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 870 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,064 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,058 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,611 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  Replace for loop to search index position illmattic 5 1,271 Sep-03-2022, 04:04 PM
Last Post: illmattic
  IndexError: list index out of range dolac 4 1,899 Jul-25-2022, 03:42 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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