-1

I'm running this code and for some reason it is only popping 1 element when there are 2 elements in the array that should be hitting the "if not inc" statement.

I've used prints to debug and it seems that the loop is breaking after the "if not inc" == true the first time and I do not want that to happen. It should continue in the loop and get back there a second time.

Have tried pass AND continue still no desired outcome

def readFile():
    with open('incidents.txt') as f:
        x = f.read().splitlines()
        print(x)

    i = 0
    for inc in x:
        print(i)
        if not inc:
            x.pop(i)
            print("if command")
            pass
        i = i + 1
        print(x)


    y = x
    return y

Original Array -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '', '']

Expected result is -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']

Actual Result is -
['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '']
Azy Sır
  • 172
  • 2
  • 12

8 Answers8

2

Using a counter variable like i, and doing i + 1, is not a good practice in Python. Read Loop Like A Native by Ned Batchelder. Whenever you are writing Python (especially loops), see if there is an idiomatic way to achieve that. That is the real beauty of languages like Python. Also, avoid modifying a list's size while looping.

If all you need is to remove empty strings, simply use filter(None, x).

xStr = ['INC123123123', 'INC222222222', 'INC333333333', 
        'INC444444444', 'INC555555555', '', '']

print(list(filter(None, xStr)))

filters signature is (function_to_apply, list_of_inputs). So, if you use

lambda x: x != ''

then it will only select the non empty stings. It is a more verbose way compared to using None. If you want to invert it, use

lambda x: x == ''`

So, if you use list(filter(lambda x: x == '', xStr)), it will only select the empty strings.

Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 1
    Faster than list comprehension, I added an example in your answer. +1 – DirtyBit Feb 06 '19 at 06:21
  • 1
    Wow thanks Nishant - I wasn't aware of the filter command this helped a lot! Just for my own knowledge is there a command that is the opposite to this. Let's say I want to filter only "None" – Azy Sır Feb 06 '19 at 08:48
  • @AzySır, Yeah nothing is hard-coded. I should have shown the explicit way of doing that :-) – Nishant Feb 06 '19 at 13:49
1

one-liner using list comprehensions:

xStr = ['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 
        'INC555555555', '', '']
print([x for x in xStr if x])

OUTPUT:

['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1

Use filter to remove particular types of strings you want to remove. You can use lambda along with filter to add condition to remove any particular type of string.

line = ['INC123123123', 'INC222222222', 'INC333333333', 
        'INC444444444', 'INC555555555', '', '']

line_mod = filter(lambda x: x != '', line)
print line_mode

Output:

['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555']
Jay
  • 24,173
  • 25
  • 93
  • 141
1

It is not a good idea to delete element from a list while iterating it.

Try:

def readFile():
    with open('incidents.txt') as f:
        result = []
        for line in f:
            line = line.strip()
            if line:
                result.append(line)
    return result
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

How about this? It filters out all the strings with length 0

def foo():
    x = ['INC123123123', 'INC222222222', 'INC333333333', 'INC444444444', 'INC555555555', '', '']
    x =  list(filter(len, x))
    y = x
    return y

print(foo())
Anurag A S
  • 725
  • 10
  • 23
0

You are trying to modify the list while traversing the same which causes problems. Instead you should do the following:

def readFile():
    with open('incidents.txt') as f:
        x = f.read().splitlines()
        print(x)

    y = [a for a in x if a]
    return y
paradocslover
  • 2,932
  • 3
  • 18
  • 44
0

Maybe you should use if inc == '' instead.

Michael T
  • 370
  • 4
  • 16
0

you would use filter function to remove blank string.

def readFile():
    with open('try.txt') as f:
        x = f.read().splitlines()
        print(x)
    i = 0
    y = list(filter(None, x))    
    return y
Abhishek Raj
  • 480
  • 4
  • 14