0

I have a list:

[['EWR', 2, 3], ['Queens', 0, 5], ['Brooklyn', 1, 1] ]

How would I extract the 0 if I have the string Queens in a var?

In other words how would i express: I want the first int in the list that starts with the word Queens.

EDIT: Queens (and strings in general) is always in first position. The list structure can't change

4 Answers4

3

IIUC you could use the following list comprehension. If you're interested in the first integer, simply index the sublist to select the second value when the first string is 'Queens':

l = [['EWR', 2, 3], ['Queens', 0, 5], ['Brooklyn', 1, 1] ]
[i[1] for i in l if i[0] == 'Queens']
# [0]
yatu
  • 86,083
  • 12
  • 84
  • 139
  • True but its not a dynamical answer – Omer Tekbiyik Apr 16 '19 at 13:56
  • From the example it seems that the string is always in the first position, and the rest are ints. If that were not the case, IMO OP would have chosen a better example contemplating other scenarios – yatu Apr 16 '19 at 13:57
  • 1
    https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects – Devesh Kumar Singh Apr 16 '19 at 13:57
  • thak you. This seems to work on the console but when I try all the script I have something like: [D[1] for i in D if i[0] == temp][0][1] I get the error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). –  Apr 16 '19 at 13:58
  • I mean the string 'Queens' may not be always 1. index . Given spesific index works just this example @yatu – Omer Tekbiyik Apr 16 '19 at 13:59
  • @wawa can you share a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? This works fine for your case. Also please clarify whether or not 'Queens' can be in other positions or always in the first – yatu Apr 16 '19 at 14:00
  • 1
    Queens (and strings in general) is always in first position. The list structure can't change –  Apr 16 '19 at 14:03
  • Okay. Note that the code you've share is different than the answer I've posted. PLease check again @wawa – yatu Apr 16 '19 at 14:06
0

If you want exactly first int and you're not sure what index it's gonna be you could this as follow using list comprehension and python builtin filter() function. Because we are taking advantage of using generators instead of lists in out list comprehension we'll always stop iterating after finding first occurrence of whether the first int value or final sentence with first value Queens.

x = next(next(filter(lambda x:isinstance(x, int), i)) for i in my_list if i[0] == 'Queens')
print(x) # -> 0
Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
0

You can probably just bruteforce it, ill make an example easy to understand:

def find_in_3d(input, your_list):
    for second_layer in your_list:
            if input in second_layer:
                return second_layer[1]
Hans Daigle
  • 364
  • 2
  • 14
0

Iterate through the list, and check the condition if Queens is present in the list, then append the second element in output list

lst = [['EWR', 2, 3], ['Queens', 0, 5], ['Brooklyn', 1, 1] ]

ext_list = []
for l in lst:
    if 'Queens' in l:
        ext_list.append(l[1])
print(ext_list)
#[0]
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40