0

I am trying to delete empty elements of a list named report info split but keep getting list out of range errors.

The list looks similar to this :

reportinfosplit = [[1,,g,5,44,f][1,5,4,f,g,,g]]

I have found many answers to this for 1d arrays but not 2d. Is there a way to prevent the index from going out of range? The list elements are not all the same size, and I thought that was the reason, but it kept happening after I made them the same length.

for i in range(len(reportinfosplit)):
    for j in range(len(reportinfosplit[i])):
        if(j<=len(reportinfosplit[i])):
            if reportinfosplit[i][j] == "":
                del reportinfosplit[i][j]
1
  • if(j<=len(reportinfosplit[i])) -> if(j<len(reportinfosplit[i])) might do the trick Commented Mar 12, 2018 at 7:17

3 Answers 3

2

You can use filter to remove empty values from list

reportinfosplit = [[1,"","g",5,44,"f"],[1,5,4,"f","g","","g"]]
print([filter(None, i) for i in reportinfosplit])     #Python3 print([list(filter(None, i)) for i in reportinfosplit])

Output:

[[1, 'g', 5, 44, 'f'], [1, 5, 4, 'f', 'g', 'g']]
Sign up to request clarification or add additional context in comments.

1 Comment

Added snippet for python3. Thanks
0

Using list comprehensions

reportinfosplit = [[1,"",'g',5,44,'f'],[1,5,4,'f','g',"",'g']]

for ix, i in enumerate(reportinfosplit):
    reportinfosplit[ix] = [val for val in i if val != '']

print(reportinfosplit)

[[1, 'g', 5, 44, 'f'], [1, 5, 4, 'f', 'g', 'g']]

4 Comments

I definitely like this solution and it works. I have always found list comprehensions difficult to read though, which is why I've had issues using them. I am currently learning python 3 transitioning from C++. I really appreciate the help!
Yeah they definitely take a moment to get used to. This goes through each row. Then the list comprehension, creates a new list which includes all the values except those that are ''.
Can you tell me more about what it means by ix? Is x just another variable in the nested loop in this case? If it is, why is it so many people use x instead of j. I was always told to use i, j, k instead of x, y, z
@MakennaSophiaHerl the for loop with enumerate outputs both the index of the instance and the instance itself. These variables can have any name you desire. I chose ix for index and i for the item.
0

You are checking j for j<=len(reportinfosplit[i]), which results in j being able to become as large as the current length of reportinfosplit[i] (which is possible, since the current length of reportinfosplit[i] changes). Try it like this:

for i in range(len(reportinfosplit)):
    for j in range(len(reportinfosplit[i])):
        if(j<len(reportinfosplit[i])):
            if reportinfosplit[i][j] == "":
                del reportinfosplit[i][j]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.