#πŸ”’ List Filtering is skipping values

31 messages Β· Page 1 of 1 (latest)

shadow patrol
#

hello , so i am trying to removing outliers from my data , but unfortunately it can remove only limited value , can someone tell me what's wrong

for value in solnL_i:
    if abs(value)>10:
        print(value)
        solnL_i.remove(value)
for i in solnL_i:
    if abs(i)>10:print("what")

no matter how many times i run this i always have printed "what" few times

tropic brambleBOT
#

@shadow patrol

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pure iris
#

it messes with the indexing

#

!e

data = ['a', 'b', 'c', 'd']

for x in data:
    data.remove(x)

print(data)
shadow patrol
#

yeah it does!!
thats why i am using element by element loop

tropic brambleBOT
shadow patrol
#

earlier i was using for i in range(len(list):
that did mess up the indexes and threw an index out of range error i realized why was it soo

#

by what is happening here ?

pure iris
#

the iteration is based on the len of the object

#

a is removed so now the list has a len of 3

#

now the list is ['b', 'c', 'd']

#

it was just at index 0, so it moves to index 1, which is 'c' now

#

now the list is ['b', 'd']

#

then it tries to move onto index 2, but there is no longer an index 2 in the list, so the loop is finished

shadow patrol
#

huh
doesnt that break the logic ?
cuz it should use element by element iteration rather than index by index

pure iris
#

this is what's happening under the hood

shadow patrol
#

well then what can i do to achieve the proper filtering
i would do filtering by list comprehension like

solnL_i=[value for value in solnL_i if abs(value) >10]
#

but my actual code involves many things

pure iris
#

yes, typically when filtering a list, you're really going to want to construct a new list that only contains the elements you care about

shadow patrol
#
for value in solnL_i:
    if abs(value)>10:
        # print(value)
        # weightL_i.pop(index:=solnL_i.index(value))
        # weight_0L_i.pop(solnL_i.index(value))
        # biasL_i.pop(solnL_i.index(value))
        # bias_0L_i.pop(solnL_i.index(value))
        solnL_i.remove(value)
        # outlier_outcome.append(value)
        # outlier_weight.append(weightL_i[index])
        # outlier_weight_0.append(weight_0L_i[index])
        # outlier_bias_0.append(bias_0L_i[index])
        # outlier_bias.append(biasL_i[index])
#

basically i wanna do many things

#

what can i do

pure iris
#

start by creating a list of values you care about

#

you can do removal, but it's a bit trickier. First you have to note down the indices you want to remove, and then iterate the list backwards to remove them (since it re-index everything to do so)

shadow patrol
#

hmph so there is no shortway

pure iris
#

if by "short way" you mean "what you were already trying", then no

shadow patrol
#

thankss!

#

!close

tropic brambleBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.