Python Forum
unable to remove all elements from list based on a condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to remove all elements from list based on a condition
#1
list=[6.2,5.9,4.8,6.1,6.1,6.5,5.9,5.8,6.2]

for a in list:
    if a<=6.0:
       list.remove(a)
      
print(list)
The output i am getting is :
Output:
[6.2, 4.8, 6.1, 6.1, 6.5, 5.8, 6.2]
Q> Why 5.8 is NOT getting removed ?
Yoriz write Jan-27-2024, 09:34 AM:
Please post all code, output and errors (in its entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Removing an item from the list while looping over it changes the index location of the remaining items.
You can loop over a copy of the list and remove items from the original list

values=[6.2,5.9,4.8,6.1,6.1,6.5,5.9,5.8,6.2]
 
for value in values[:]:
    if value<=6.0:
       values.remove(value)
       
print(values)
Output:
[6.2, 6.1, 6.1, 6.5, 6.2]
Also note that you shouldn't use list as the variable name to contain your list as it overwrites the keyword list
sg_python likes this post
Reply
#3
As Yoriz said, it is not good to remove items from a list while you are looping through it. Will cause problems!

Nor is it good to use the reserved word list as you did.

You could approach this from the other way round, instead of slicing out a copy, create a new list to start with and append:

mylist=[6.2,5.9,4.8,6.1,6.1,6.5,5.9,5.8,6.2]
mynewlist = [] 
for a in mylist:
    if a>=6.0:
       mynewlist.append(a)
print(mynewlist)
Output:
[6.2, 6.1, 6.1, 6.5, 6.2]
I think it is a good idea to preserve the original list for later use perhaps.
sg_python likes this post
Reply
#4
A list comprehension would work well here
values = [6.2, 5.9, 4.8, 6.1, 6.1, 6.5, 5.9, 5.8, 6.2]
print([value for value in values if value > 6])
sg_python and Pedroski55 like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 1,208 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 490 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Sent email based on if condition stewietopg 1 869 Mar-15-2023, 08:54 AM
Last Post: menator01
  Checking if a string contains all or any elements of a list k1llcod3 1 1,114 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  create new column based on condition arvin 12 2,255 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  Remove numbers from a list menator01 4 1,346 Nov-13-2022, 01:27 AM
Last Post: menator01
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 849 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to change the datatype of list elements? mHosseinDS86 9 2,010 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,168 May-17-2022, 11:38 AM
Last Post: Larz60+
  Replace elements of array with elements from another array based on a third array Cola_Reb 6 1,881 May-13-2022, 06:06 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