Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'testing,': 0.07; '(1,': 0.09; 'subject:set': 0.09; '(say': 0.16; 'subject:object': 0.16; 'x).': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'solution.': 0.18; '>>>': 0.18; "i've": 0.23; 'header:In-Reply-To:1': 0.25; 'set.': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'seemingly': 0.29; 'objects': 0.29; 'fri,': 0.30; 'could': 0.32; 'true.': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'received:google.com': 0.34; 'needed': 0.35; 'false': 0.35; 'problem,': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'containing': 0.61; 'dear': 0.66; '2013': 0.84; 'dict.': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=i+DpWVcYUYrVgy0UcMg3rVKd2j9wveA0awq5AKf4tgc=; b=K7Fcdp8yyt4RglR3PdQM/P+kP/3A5VIIXIOiQQ/fAfqwKuLO6WJNdjdWO8fmmcPky1 P9E1Q/d3xDuMUW+Mp5NAQok/HWibJ0QHgTb4OnEpBlMX/yVjVHo3pqJH+azhWzqTEY4v +oBpBGaEhBD3a/nVbqNFbHQatZ5W+KysMRWrt94ZauNzdsTO5WZo37v9xVUIDGmxjl6Y Y+YufR0TGpR272Q/xh23UWlNZwudZ4CuqYNS1ufiGzicHV4uHbkC+TqFnrM59DjgrdST DhGrsxFKfcOSbQwHoCWqnkP6tkETaOSkfFKsOB/XKqNbANsBqAIM7QGXhtukX5AZV/p7 dDaw== X-Received: by 10.66.87.67 with SMTP id v3mr17211108paz.63.1359157052801; Fri, 25 Jan 2013 15:37:32 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Fri, 25 Jan 2013 16:37:02 -0700 Subject: Re: Retrieving an object from a set To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359157062 news.xs4all.nl 6894 [2001:888:2000:d::a6]:60565 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37699 On Fri, Jan 25, 2013 at 4:30 PM, Ian Kelly wrote: > On Fri, Jan 25, 2013 at 4:14 PM, Arnaud Delobelle wrote: >> Dear Pythoneers, >> >> I've got a seemingly simple problem, but for which I cannot find a >> simple solution. >> >> I have a set of objects (say S) containing an object which is equal to >> a given object (say x). So >> >> x in S >> >> is true. So there is an object y in S which is equal to x. My >> problem is how to retrieve y, without going through the whole set. > > You could use a dict. > >>>> y = (1, 2, 3) >>>> S = {x: x for x in [y] + range(10000)} >>>> x = (1, 2, 3) >>>> x in S > True >>>> x is y > False >>>> S[x] is y > True Or you could use a set intersection: >>> S = set([y] + list(range(10000))) >>> S.intersection([x]).pop() (1, 2, 3) In my testing, the time needed for this is small and does not seem to depend on the size of the set.