Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'example:': 0.03; 'memory.': 0.05; 'integers': 0.09; 'simplest': 0.16; 'symmetric': 0.16; 'cc:addr:python-list': 0.16; 'subject:list': 0.18; '>>>': 0.18; 'cc:no real name:2**0': 0.20; 'memory': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'creating': 0.25; 'concern': 0.28; 'lists': 0.28; 'problem': 0.28; 'second': 0.29; 'cc:addr:python.org': 0.30; 'least': 0.31; 'list': 0.32; 'difference': 0.34; 'header:User-Agent:1': 0.34; 'lists,': 0.35; 'totally': 0.35; 'example,': 0.37; 'list,': 0.37; 'but': 0.37; 'something': 0.37; 'some': 0.38; 'subject:: ': 0.39; 'received:192': 0.39; 'sets': 0.39; 'million': 0.74; '[1,2]': 0.84; 'error-free': 0.84; 'union,': 0.91; 'reliable,': 0.93 Date: Tue, 16 Aug 2011 08:51:25 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11 MIME-Version: 1.0 To: Roy Smith Subject: Re: testing if a list contains a sublist References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313477497 news.xs4all.nl 23896 [2001:888:2000:d::a6]:48173 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11513 >> 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]) >>> Although with big lists, this is not very memory efficient. But I must tell you, sometimes I use this method for lists with millions of integers, and it is very fast and reliable, and memory is not a concern for me, at least - some million integers will fit into a few MB of memory. Read the docs about set operators for creating union, symmetric difference etc. Best, Laszlo