2

I would like a way to find which element in a list is not ""
For example, for this:
['','','b']
It should return 2 because the index of "b" is 2

user36456453765745
  • 129
  • 1
  • 2
  • 6
  • 1
    Possible duplicate of [How to count the frequency of the elements in a list?](http://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-a-list) – umutto Apr 05 '17 at 03:21
  • @umutto: No, that's quite different. That's asking about frequency for every character; this is about the index of the first character that is different. – zondo Apr 05 '17 at 15:56

4 Answers4

1

The following uses enumerate to tie each element to a numeric index, then we filter the array for non-empty strings.

If you want all instances:

test = ['','','b'] 
print(filter(lambda a: a[1] != "", enumerate(test)))

Returns

[(2, 'b')]

If you only want the first instance:

test = ['','','b'] 
print(filter(lambda a: a[1] != "", enumerate(test))[0][0])

If you want a list of all instances:

test = ['','','b'] 
print([ele[0] for ele in filter(lambda a: a[1] != "", enumerate(test))])
Neil
  • 14,063
  • 3
  • 30
  • 51
  • The first one returns: The second one returns: Traceback (most recent call last): File "", line 1, in print(filter(lambda a: a[1] != "", enumerate(test))[0][0]) TypeError: 'filter' object is not subscriptable – user36456453765745 Apr 05 '17 at 03:28
  • The code compiles correctly on my end. What version of python are you using? And are you trying my exact code? – Neil Apr 05 '17 at 03:31
1

A nice, fast way can use next() with enumerate() and a generator expression:

next(index for index,item in enumerate(mylist) if item != '')

That is very efficient; it stops checking as soon as it finds a match. If everything in the list is '', it will throw a StopIteration exception. If you want to have a default value, say -1:

next((index for index,item in enumerate(mylist) if item != ''), -1)
zondo
  • 19,901
  • 8
  • 44
  • 83
0

You can use a simple for loop and check if the value is not '' then save the index of the value within a list comprehension to solve your problem

test = ['','','b'] 
print([test.index(x) for x in test if x != ''][0]) # output 2
Taku
  • 31,927
  • 11
  • 74
  • 85
  • No, I want it to return the index value of 'b' – user36456453765745 Apr 05 '17 at 03:27
  • @user36456453765745 there, but please make your question clear on your initial post next time. – Taku Apr 05 '17 at 03:30
  • @abccd Actual, I think it was pretty clear what the OP wanted from the beginning. He said _"I would like a way to find which element in a list is not `""` "_ not _"I would like a way to find **how many** elements in a list are `""`"_. But more importantly, if you were not exactly sure what the OP wanted, why did you answer in the first place? Unclear questions should be closed until they become clear, instead of trying to guess at what the correct answer would be. – Christian Dean Apr 05 '17 at 03:39
0

All the other answers looks concise , but it will iterate over the entire list even if the first element is not ''. If you just need the first element which is not '', I think you should use a simple for loop.

def function(a,k):
    for i, j in enumerate(a):
        if j != k:
            return i

a = ['','','b']
k=''
print(function(a,k))
Himaprasoon
  • 2,609
  • 3
  • 25
  • 46