Python Forum
List Comparisons and Deleting Valuebles
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Comparisons and Deleting Valuebles
#1
So i have 2 lists. when the value in list_a is not between 75 and 400 it has to remove the value in the list_a and list_b in the same place. I dont know why it gives this strange output? I hope you guys can help :)

#input
list_a = [50,100,200,500,50]
list_b = [1,2,3,4,5]

#code
for i,j in zip(list_a,list_b):
    if 75<i<400:
        list_a.remove(i)
        list_b.remove(j)
print(list_a,list_b)

#output 
[50, 200, 500, 50] [1, 3, 4, 5]

#prefered output
[100,200] [2,3]
Reply
#2
list.remove takes as argument the value you want to remove, not the index. What you are probably looking for is del (delete). For this purpose list.pop is also commonly used, but in your case it won't make much difference.
Reply
#3
But when I change the code to the del function, it gives an error: "list assignment index out of range"

list_a = [50,100,200,500,50]
list_b = [1,2,3,4,5]

#code
for i,j in zip(list_a,list_b):
    if 75<i<400:
        del list_a[i]
        del list_b[j]
print(list_a,list_b)

#IndexError: list assignment index out of range
Reply
#4
The code will require a bit of modification. Note that indices in Python start with 0, and items in your list_b start with 1. And of course, deleting element with index "i" will be no good. Examine the code a bit, you will quickly figure why.
Reply
#5
Your initial code did not work because you were removing the first occurence of i - instead of the one at position matching j (one-letter variables should be a punishable offence Doh )

(May-13-2018, 01:27 PM)KaleBosRatjes Wrote: But when I change the code to the del function, it gives an error: "list assignment index out of range"

list_a = [50,100,200,500,50]
list_b = [1,2,3,4,5]

#code
for i,j in zip(list_a,list_b):
    if 75<i<400:
        del list_a[i]
        del list_b[j]
print(list_a,list_b)

#IndexError: list assignment index out of range

Your i and j are list elements and you try to use them as list indices.

Regardless, deleting from lists is heavy and inefficient operation - O(n), building new lists is usually more efficient.

list_a = [50,100,200,500,50]
list_b = [1,2,3,4,5]

combined = [[key, val] for key, val in zip(list_a, list_b) if 75 < key < 400]
print(list(zip(*combined)))
And the result is - tada!:
Output:
[(100, 200), (2, 3)]
PS Please, read PEP-8
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  deleting select items from a list Skaperen 13 4,565 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  Deleting employee from list SephMon 3 3,283 Jan-05-2021, 04:15 AM
Last Post: deanhystad
  Problem with readlines() and comparisons dudewhoneedshelp 2 2,181 Jul-23-2020, 10:21 AM
Last Post: DeaD_EyE
  Comparisons with functions menator01 1 1,843 Jun-07-2020, 09:28 AM
Last Post: Gribouillis
  Deleting the first item in linked list dan789 7 4,019 Mar-05-2019, 06:34 PM
Last Post: ichabod801
  Deleting from a list of objects buddih09 2 2,645 Nov-07-2018, 04:08 PM
Last Post: ichabod801
  deleting certain rows from multidimensional list aster 4 14,377 Nov-05-2017, 10:52 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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