Function to intersect two lists : List Intersect « List « Python





Function to intersect two lists

Function to intersect two lists

def intersect(seq1, seq2):
    res = []                     # start empty
    for x in seq1:               # scan seq1
        if x in seq2:            # common item?
            res.append(x)        # add to end
    return res

x = intersect([1, 2, 3], (1, 4))    # mixed types
print x                                   # saved result object

           
       










Related examples in the same category

1.Define function to intersect Strings and listsDefine function to intersect Strings and lists