Forums

list problem

Hi everyone, I'm trying to create a function that locates a number that is in a list of lists.

So I have my list called superlist= [[1,3,7,9],[2,8,3,5],[4,1],[6]]

and I want to create a function, locate(superlist, number)

such that for a chosen number, the output will give me which lists in the superlist that number belongs too.

For example, locate(superlist,1) should output list 1 and list 3.

Like this?

def locate(superlist, number):
    return [n+1 for n, x in enumerate(superlist) if number in x]

This function returns the list number, as you implied you wanted. If you in fact wanted the list of lists, that's even easier

def locate(superlist, number):
    return [x for x in superlist if number in x]

Thanks Cartroo,

How would I index each of the lists automatically as List1,List2,List3,List4 so that the particular index e.g. (List2,List4) is returned.

Could i index it as follows

For range(i=1,4,1): List(i) =Superlist[i] ?

I'm not sure exactly what you want to do - could you give an example of what you're using this for?