Finding a list in list python

Viewed 108

I have a list that contains other lists as elements.

mylist=[
         [1, 'Asfoor', 'a', 'b', 'c'], 
         [2, 'deek', 'j', 'a', 'k'], 
         [3, 'bata', 'k', 'a', 'p'],
         [4,'farkha','v','m','k']
       ]

Now, I want to find occurrences of another list elements 'a' and 'k' in list above and need the output to be in certain format.

second list=['a','k']

For example if any element of the first list contain 'a' element of the second list then output list should be something like this:

['a',
     [
         [1, 'Asfoor', 'a', 'b', 'c'], 
         [2, 'deek', 'j', 'a', 'k'], 
         [3, 'bata', 'k', 'a', 'p']
     ]
]

And similarly, if list contains 'k' then output as below:

['k',
         [2, 'deek', 'j', 'a', 'k'], 
         [3, 'bata', 'k', 'a', 'p'],
         [4,'farkha','v','m','k']
]

Any good python way to do it would be highly appreciated.

3 Answers
Related