Common elements comparison between 2 lists.

Posted by Daniel on Stack Overflow See other posts from Stack Overflow or by Daniel
Published on 2010-05-19T10:57:01Z Indexed on 2010/05/19 11:00 UTC
Read the original article Hit count: 168

Filed under:
|
def common_elements(list1, list2):
    """
    Return a list containing the elements which are in both list1 and list2

    >>> common_elements([1,2,3,4,5,6], [3,5,7,9])
    [3, 5]
    >>> common_elements(['this','this','n','that'],['this','not','that','that'])
    ['this', 'that']
    """
    for element in list1:
        if element in list2:
            return list(element)

Got that so far, but can't seem to get it to work! Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about lists