-3

I'm having trouble in removing some empty indexes from a list of lists. It was a list with letters and values, and I only needed the values, but some indexes of the list had only letters, and removing them left some empty indexes on my list.

Originally it was a list of strings with letters and numbers.

The list:

[['29882.09'],[],['81005.79'],[],['14700.0']]

And some of the solutions I tried, that either didn't change anything or gave me an entirely empty list.

This one, didn't change anything on the list:

list = [x for x in list if x != []]

This one didn't change anything either:

for item in list:
            if item!=[]:
                list2.append(item)

And this one just gave me back an entirely empty list:

for i in range(len(list)):
            list[i] = list(filter(None, list[i]))

With those empty indexes, I can't use .append of openpyxl because it says the index is out of range.

The line that gives an error with the list with empty indexes:

ws.append({'A': list_names, 'B': list_values[0]})
wb.save('doc.xlsx')

And it gives me is "IndexError list index out of range".

This is part of code where I'm converting a PDF to XLSX (this is the part of the conversion code and the separation values to columns), to compare values from two different columns on different XLSXs and create a third one with the missing values. I can't just edit the code to skip certain indexes because I need it to work with other PDFs with the same model.

I found out why the code isn't working, apparently the empty brackets are considered to be on the same index as the index above, so the real list would be:

[['29882.09'][],['81005.79'][],['14700.0']]

I found this out by doing print(len(item)).

Is there a way to remove those empty brackets at the end?

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
  • 3
    To avoid ambiguity, it would be helpful if you edited the first code block as a list (meaning commas and outside brackets) if that's what you are really starting with. – Mark May 31 '22 at 20:00
  • 2
    Your first two examples seem to work just fine. I think your `filter` example needs a little work, but you already have two other functional options. If they're not working for you, it helps to provide details about the *way* in which they're not working: are you getting errors? Unexpected/undesired output? – larsks May 31 '22 at 20:02
  • >{With those empty indexes i cant use .append of openpyxl because it says the index is out of rang} Could you elaborate on this? Can you show your actual code and the error you're getting? – decorator-factory May 31 '22 at 20:17
  • My bad, wrote it on a hurry while leaving work, hope this edit gives more explanation, none of the answer actually worked, not sure why, it only gave the same list again with no modifications – João Pedro May 31 '22 at 20:52
  • One important note: it's a good idea to avoid using built-in type names (such as `list`) as variable names, as this can lead to unexpected behavior. – constantstranger May 31 '22 at 22:58
  • The list name was just a demonstration, iam actually using vend_name and vend_value, anyway, found out why the code wasn't working, just need a different solution now – João Pedro Jun 01 '22 at 12:31

3 Answers3

-1

The most pythonic way:

>>> l = [[29882.09],[],[81005.79],[],[14700.0]]
>>> l = [x for x in l if x]
>>> l
[[29882.09], [81005.79], [14700.0]]
Burak Özalp
  • 53
  • 2
  • 8
-1

try this code:

lst = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]

# Method 1: List Comprehension
print([x for x in lst if x])

# Method 2: filter()
print(list(filter(lambda x: x, lst)))
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
-1

This should do it:

lst = [
    [29882.09],
    [],
    [81005.79],
    [],
    [14700.0]
]

lst = [x for x in lst if x]
print(lst)

Output:

[[29882.09], [81005.79], [14700.0]]
constantstranger
  • 4,502
  • 2
  • 2
  • 15