+ 2

How can I find a item in a list that's in a list?

To find a item in a list you would do: List = ["hi","hello","bye"] List.index("hello") >>>1 How do I find something that is in a list that's in a list and give me both values: (doesn't work btw, just example) List = [["hi","hello"],["bye"]] List.index("hello") >>>0,1

25th Jan 2017, 11:06 PM
Jared
Jared - avatar
2 Respuestas
0
# If you are looking to retrieve values of indexed positions # in a nested list # you can access the outer level list elements with the first [index] # the sub level list elements with the [second] index and so forth. # depending on the degree of list nesting you have. my_list = [["hi","hello"],["bye"]] get_hello = my_list[0][1] print(get_hello) get_bye =my_list[1][0] print(get_bye) # if want the indexed locations and associated values # for a list of lists one option is looping over the enumerated # lists for ind,item in enumerate(my_list): for indx,subitem in enumerate(item): print('found item {0} at [{1}][{2}]'.format(subitem,ind,indx))
26th Jan 2017, 6:30 AM
richard