2

I am a newbie in python and I was trying to figure out, how do I differentiate between 2 lists which are as shown below

['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', '', '', 'German', 'Name']

And

['', '', '', '', '', '', '', '']

The Problem is, that both the lists have '' element, and I want a solid Condition which satisfies that if a list has an item which is a string and not ''. It is also possible that list has 7 '' and just one item is a string.

3
  • filter(None, your_list) might be what you are looking for. Commented Sep 15, 2017 at 5:58
  • i need a condition. Commented Sep 15, 2017 at 6:05
  • 1
    What is your expected output? Commented Sep 15, 2017 at 6:28

4 Answers 4

2

You can just use any with the list as argument :

>>> any(['', '', '', '', '', '', '', ''])
False
>>> any(['', '', '', '', '', '', '', 'Test', ''])
True

If there's any element which is truthy (i.e. non empty), it will return True.

Sign up to request clarification or add additional context in comments.

1 Comment

This wasn't exactly what I wanted, but it was the closest to my expected output.
1

It seems you want to filter empty strings from a list:

lst = ['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', '', '', 'German', 'Name']
[item for item in lst if item]
# ['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', 'German', 'Name']

I want a solid Condition which satisfies that if a list has an item which is a string and not ''

The condition is if item. To clarify, '' is an empty string. During iteration, if the item is '', the condition is False and thus the item is excluded from the result. Otherwise, the condition is True and the result is added to the list. See also this post.

This behavior is because all objects in Python have "truthiness" - all objects are assumed True except for a few, such as False, 0, "", None and empty collections.

2 Comments

Shouldn't it be same as filter(None, lst)? Please see my earlier comment.
@Unni, yes list(filter(None, lst)) gives the same output in a different way. filter is the functional approach that uses an inherent default predicate, which may not be obvious to the OP. Here I am using a list comprehension to demonstrate that '' is an empty, Falsey string, which gets filtered because it negates the condition. Still, I feel the OP may be looking for something else, which requires clarification in the question.
0

I need a condition which is satisfied if there are no '', in the string.

You can use all for checking this.

In [1]: s1 = ['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', '', '', 'German', 'Name']
In [2]: s2 = ['11-10-2017', '12:15 PM']

In [4]: all(x for x in s1)
Out[4]: False

In [5]: all(x for x in s2)
Out[5]: True

1 Comment

I need a condition which is satisfied if there are no '', in the string.
0

The easiest way to see if there is not a '' in your list is to use not in:

tests = [
    ['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', '', '', 'German', 'Name'], 
    ['', '', '', '', '', '', '', ''], 
    ['a', 'b', 'c'],
    ['', '', '', '', '', 'x', '', '']]

for t in tests:
    print '' not in t, t

Which would display:

False ['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', '', '', 'German', 'Name']
False ['', '', '', '', '', '', '', '']
True ['a', 'b', 'c']
False ['', '', '', '', '', 'x', '', '']

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.