dazzlingk
dazzlingk

Reputation: 53

Removing rows that contain empty column without pandas

a_list = [ ["a", ""], ["b", "2"] ]

I have a list of lists as written above. Any suggestions on how to remove the row that contains an empty element (in this case the first list), without using pandas, so it returns:

a_list = [ ["b", "2"] ]

Upvotes: 4

Views: 98

Answers (2)

Devil
Devil

Reputation: 1174

To check at specific index too

Here in your case you're check list[xx][1] index

solution-1

#Devil 
a_list = [["a", ""], ["b", "2"]]
a_list = [l for l in a_list if l[1] != ""]
print(a_list)

solution 2

Use another array and append the data init

new = []
for i in l:
    if i[1] != "":
        new.append(i)
print(new)

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195408

Try:

a_list = [["a", ""], ["b", "2"]]

a_list = [l for l in a_list if "" not in l]
print(a_list)

Prints:

[['b', '2']]

Upvotes: 4

Related Questions