List: Find the first index and count the occurrence of a specific list in list of lists

Advertisements

we have a variable named location.

location=[["world", 'Live'], ["alpha",'Live'], ['hello', 'Scheduled'],['alpha', 'Live'], ['just', 'Live'], ['alpha','Scheduled'], ['alpha', 'Live']]

i want to find the first index and count occurrence of list["alpha",’Live’] in location.
i tried the following:

index= [location.index(i) for i in location if i ==["alpha", 'Live'] ]
count = [location.count(i) for i in location if i ==["alpha",'Live'] ]
print('index',index)
print('count', count)

this returns:
index [1, 1, 1]
count [3, 3, 3]

but is there a way to find both first index, count simultaneously using list comprehension.

expected output:

index, count = 1, 3

>Solution :

does this solve you problem?

location=[["world", 'Live'], ["alpha",'Live'], ['hello', 'Scheduled'],['alpha', 'Live'], ['just', 'Live'], ['alpha','Scheduled'], ['alpha', 'Live']]
index= location.index(["alpha",'Live'])
count = location.count(["alpha",'Live'])
print('index',index)
print('count', count)

Leave a ReplyCancel reply