##### Compare Two Lists and Replace Items In a List by Index
 Compare Two Lists and Replace Items In a List by Index nagymusic Programmer named Tim Posts: 13 Threads: 5 Joined: Nov 2018 Reputation: May-09-2020, 10:13 PM 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 SheeppOSU Minister of Silly Walks Posts: 465 Threads: 85 Joined: Feb 2018 Reputation: May-09-2020, 10:40 PM (This post was last modified: May-09-2020, 10:41 PM by SheeppOSU.) The best option here is a for loop. For loops are used to loop through a list or through something with a set number of loops. We have a set number of loops here which is the list, so a for loop would work best. I'd rather that you solve this yourself so I'll explain to you how you can implement this. First you need to create the `list_c`. Next, you should loops through `list_b` since that's the one you're checking for negative numbers. If you loop through the elements in `list_b` you would have to create an index variable yourself which is extra code, so to make it more efficient I suggest the following: `for index in range(0, len(list_b))`, then the elements from `list_a` and `list_b` can be accessed using `list_a[index]` and `list_b[index]`. So the code inside the for loop should look to see if the element of `list_b` is a negative number, if it isn't then do nothing, otherwise: `list_a[index] = None`. Hope this help Age Doesn't Matter, Your Passion Does - Shreya Jain Reply deanhystad Da Bishop Posts: 1,974 Threads: 11 Joined: Feb 2020 Reputation: May-10-2020, 05:28 AM This could be a fun little list comprehension. Use zip to pull the two lists together. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Feed List items with Integer euras 1 30 4 hours ago Last Post: menator01 List of lists - merge sublists with common elements medatib531 1 185 May-09-2021, 07:49 AM Last Post: Gribouillis Sort List of Lists by Column Nju 1 278 Apr-13-2021, 11:59 PM Last Post: bowlofred Parse String between 2 Delimiters and add as single list items lastyle 5 279 Apr-11-2021, 11:03 PM Last Post: lastyle IndexError: list index out of range brunolelli 11 584 Mar-25-2021, 11:36 PM Last Post: brunolelli Changing Index of 2 List in python giddyhead 0 230 Mar-05-2021, 05:45 PM Last Post: giddyhead Sum of list items tristanfermat 2 371 Feb-06-2021, 12:04 PM Last Post: tristanfermat IndexError: list index out of range ramu4651 2 440 Jan-24-2021, 01:45 PM Last Post: buran Matching variable to a list index Gilush 17 870 Nov-30-2020, 01:06 AM Last Post: Larz60+ Creating list of lists from generator object t4keheart 1 386 Nov-13-2020, 04:59 AM Last Post: perfringo

Forum Jump:

### User Panel Messages

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