How to check if a list contains empty items?

Suppose I have an empty string, it will be split:

>>>''.split(',') [''] 

The result of the split [''] . I use bool to check if it is empty. It will return True .

 >>>bool(['']) True 

How to verify that the split result is empty?

+5
source share
5 answers

With bool(['']) you check to see if the contents of what it does in the list [''] , the content will simply be an empty string. ''

If you want to check if all the elements in the list are “empty” (therefore, if the list contains a string '' , it will return False ), you can use the built-in function all() :

 all(v for v in l) 

This element takes every v element in list l and checks to see if it is True ; if all elements return True , if at least one of them does not return False . As an example:

 l = ''.split(',') all(v for v in l) Out[75]: False 

You can replace this with any() to do a partial check and see if any of the items in the list l are set to True .

A more detailed example * for both uses:

 l = [1, 2, 3, ''] all(l) # '' doesn't have a True value Out[82]: False # 1, 2, 3 have a True value any(l) Out[83]: True 

* As noted in the comments of @ShadowRanger , the same can be done with all(l) or any(l) , since they both just take iterability to the end.

+7
source

In your case, it really is not empty

If you want to check if an element is empty inside this list, you can:

 string = ''.split(',') if not string[0]: print "empty" 
+1
source

If emptiness is an important result, it is best to check the source string first:

 x = '' if x: # Original string was non-empty, split it splitx = x.split(',') if any(splitx): # There was at least one character in the original string that wasn't a comma 

In the first test, empty start lines are excluded, the second using any excludes lines that are nothing more than a split character, and therefore returned a whole bunch of empty lines, but not non-empty lines. As long as you have one non-empty line, it goes through.

Note. If you are trying to parse CSV files, do not use .split(',') ; there is a csv module that handles this correctly (including escape sequences, quotation marks, etc.) and should ALWAYS be used to parse CSV, never collapse your own parser. Added bonus: csv converts '' entries to strings [] , which you can check for truth directly, and not [''] like str.split . Example:

 >>> import csv, io >>> f = io.StringIO('\n\na,b,c\n1,2,3\n\n') >>> [row for row in csv.reader(f) if row] # Stripping easily [['a', 'b', 'c'], ['1', '2', '3']] 

vs. same approach with str.split(',') that still does not handle quotes, escaping, etc .:

 >>> f = io.StringIO('\n\na,b,c\n1,2,3\n\n') >>> stripped = (line.rstrip('\r\n') for line in f) # Must manually strip line endings first >>> [line.split(',') for line in stripped if line] [['a', 'b', 'c'], ['1', '2', '3']] 
+1
source

The separation result is not empty. The feeling of "emptiness" that you are looking for is best verified by looking at the original, unallocated string:

 if not original_string: # It empty. 

But if you really want to see the split result for this:

 if len(split_result) == 1 and not split_result[0]: # It "empty". 
0
source

According to the str.split() documentation, "Splitting an empty string with the specified delimiter returns ['']."

To check this case when you are using an explicit delimiter (for example, using split(",") ), follow these steps:

 l = s.split(",") if len(l) == 1 and l[0] == '': print("string was empty") 
0
source

Source: https://habr.com/ru/post/1242146/


All Articles