On Aug 16, 4:51 pm, Laszlo Nagy <gand...@shopzeus.com> wrote: > >> hi list, > >> what is the best way to check if a given list (lets call it l1) is > >> totally contained in a second list (l2)? > > >> for example: > >> l1 = [1,2], l2 = [1,2,3,4,5] -> l1 is contained in l2 > >> l1 = [1,2,2,], l2 = [1,2,3,4,5] -> l1 is not contained in l2 > >> l1 = [1,2,3], l2 = [1,3,5,7] -> l1 is not contained in l2 > > >> my problem is the second example, which makes it impossible to work with > >> sets insteads of lists. But something like set.issubset for lists would > >> be nice. > > >> greatz Johannes > > Fastest, error-free and simplest solution is to use sets: > > >>> l1 = [1,2] > >>> l2 = [1,2,3,4,5] > >>> set(l1)-set(l2) > set([]) > >>> set(l2)-set(l1) > set([3, 4, 5])
Error free? Consider this stated requirement: > l1 = [1,2,2,], l2 = [1,2,3,4,5] -> l1 is not contained in l2 >>> l1 = [1,2,2] >>> l2 = [1,2,3,4,5] >>> set(l1)-set(l2) set() >>> set(l2)-set(l1) {3, 4, 5} So set([1,2]) == set([1,2,2]), despite [1,2,2] not being a sublist of the original. It also completely ignores list order, which would make [9,8,7] a sublist of [5,6,7,8,9]. -- http://mail.python.org/mailman/listinfo/python-list