5

I have example list like this:

example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']]

Now, I check if it has empty string like this:

has_empty = False;
for list1 in example_list:
    for val1 in list1:
        if val1 == '':
            has_empty = True

print(has_empty)

This works OK as it prints True, but looking for more pythonik method?

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
user3654650
  • 5,283
  • 10
  • 27
  • 28
  • To be honest, this feels like more of a code review question, but I'm not so sure. Its so short of a snipped of code to be "reviewed". – Elias Benevedes Nov 13 '14 at 06:06

5 Answers5

18

You can use itertools.chain.from_iterable:

>>> from itertools import chain
>>> example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']]
>>> '' in chain.from_iterable(example_list)
True

Just in case if the inner lists are bigger(more than 100 items) then using any with a generator will be faster than the above example because then the speed penalty of using a Python for-loop is compensated by the fast in-operation:

>>> any('' in x for x in example_list)
True

Timing comparisons:

>>> example_list = [['aaa']*1000, ['fff', 'gg']*1000, ['gg']*1000]*10000 + [['']*1000]
>>> %timeit '' in chain.from_iterable(example_list)
1 loops, best of 3: 706 ms per loop
>>> %timeit any('' in x for x in example_list)
1 loops, best of 3: 417 ms per loop

# With smaller inner lists for-loop makes `any()` version little slow

>>> example_list = [['aaa'], ['fff', 'gg'], ['gg', 'kk']]*10000 + [['']]
>>> %timeit '' in chain.from_iterable(example_list)
100 loops, best of 3: 2 ms per loop
>>> %timeit any('' in x for x in example_list)
100 loops, best of 3: 2.65 ms per loop
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

You can do use combination of any, map and chain:

In [19]: example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']]

In [20]: import operator, itertools

In [21]: any(map(operator.not_, itertools.chain(*example_list)))
Out[21]: True

In [22]: example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['not empty', 'gg']]

In [23]: any(map(operator.not_, itertools.chain(*example_list)))
Out[23]: False
Marcin
  • 215,873
  • 14
  • 235
  • 294
2

Just convert the whole list to string and check for the presence of empty strings, i.e. '' or "" in it.

>>> example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']]
>>> any(empty in str(example_list) for empty in ("''", '""'))
True
>>> example_list = [['aaa'], ['fff', 'gg'], ['ff'], [ 'gg', '\'']]
>>> any(empty in str(example_list) for empty in ("''", '""'))
False

Note that this won't work with lists which have empty string as part of the string itself - example 'hello "" world'

Another approach could be to flatten the dict and check for presence of empty strings in it

>>> example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['gg', 'hello "" world']]
>>> '' in [item for sublist in example_list for item in sublist]
False
>>> example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg', 'hello "" world']]
>>> '' in [item for sublist in example_list for item in sublist]
True
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
1

using map and lambda:

>>> def check_empty(l):
...     k=map(lambda x: '' in x and True,l)
...     for x in k:
...         if x==True:
...             return True
...     return False
... 
>>> check_empty(example_list)
False
>>> example_list
[['aaa'], ['fff', 'gg'], ['ff'], ['gg', 'hello "" world']]
>>> example_list = [['aaa'], ['fff', 'gg',''], ['ff'], ['gg', 'hello "" world']]
>>> check_empty(example_list)
True
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • For the record, this is basically the implementation of [`any()`](https://docs.python.org/3/library/functions.html#any). So it is equivalent to simply doing `any('' in x for x in l)` – Tomerikoo Oct 18 '21 at 14:02
0
example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']]

has_empty = False

for list in example_list:

    if '' in list:
        has_empty = True
print(has_empty)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504