Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'subject:two': 0.07; 'jan': 0.12; 'be:': 0.16; 'inputs': 0.16; 'itertools': 0.16; 'skip:[ 40': 0.16; 'subject:based': 0.16; 'subject:key': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'import': 0.22; '31,': 0.24; 'alternate': 0.24; 'header:In-Reply-To:1': 0.27; 'possibility': 0.29; 'specified': 0.30; 'message-id:@mail.gmail.com': 0.30; 'subject:per': 0.31; 'though.': 0.31; 'lists': 0.32; 'another': 0.32; 'skip:d 20': 0.34; 'subject:from': 0.34; 'could': 0.34; 'equal': 0.35; 'subject:lists': 0.35; 'received:google.com': 0.35; 'two': 0.37; 'jason': 0.38; 'mapping': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'different': 0.65; 'results': 0.69; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=oZMbkMmyqZ9zBJEA0LIZUxO6HVbMW4fhgA1ji2CsPyo=; b=e+ByGXiJptMqz64StoABWezAGTzRyH43eWiD+ulRfA3SasG9cnemxHuSDNw2D1vrbW xpoERICZ+iA2RlWkcOJcx+MaODe+KaU8TxnJlxMVYxfEF5kn+bXd8mBxHDTNuiSl10U+ nQxHRrMC3YbltZSbM9+DzSEaCLEfIDjr+Kc/7pul2VQTNmHerRv3Pg/ARcOx1luMrAOJ mPGRV8QJPXUCRocInYomvzuZnX3H4NunpJtnY0islxiPdsyKKdAD0Nw+gWD9f9LBZVCO J+yUUCb6D0kydpA/+VR5ks7BCvmpJPuoWKR5LZ73UT9xNl0TizNWrHfRbhJd9HaBegQ7 YEAQ== X-Received: by 10.70.90.100 with SMTP id bv4mr23977753pdb.29.1422816755999; Sun, 01 Feb 2015 10:52:35 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <0dddee06-233b-436a-be48-3c16e62c1718@googlegroups.com> From: Ian Kelly Date: Sun, 1 Feb 2015 11:51:55 -0700 Subject: Re: Create dictionary based of x items per key from two lists To: Python Content-Type: text/plain; charset=UTF-8 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422816759 news.xs4all.nl 2859 [2001:888:2000:d::a6]:48370 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85018 On Sat, Jan 31, 2015 at 7:38 PM, Jason Friedman wrote: >> I have two lists >> >> l1 = ["a","b","c","d","e","f","g","h","i","j"] >> l2 = ["aR","bR","cR"] >> >> l2 will always be smaller or equal to l1 >> >> numL1PerL2 = len(l1)/len(l2) >> >> I want to create a dictionary that has key from l1 and value from l2 based on numL1PerL2 >> >> So >> >> { >> a:aR, >> b:aR, >> c:aR, >> d:bR, >> e:bR, >> f:bR, >> g:cR, >> h:cR, >> i:cR, >> j:cR >> } > > Another possibility is: > import itertools > my_dict = {x:y for x,y in zip(list1, itertools.cycle(list2))} That results in a different mapping than the one specified by the OP, though. An alternate itertools approach could be: dict(zip_longest(l1, chain.from_iterable( map(partial(repeat, times=len(l1) // len(l2)), l2)), fillvalue=l2[-1])) although this requires the inputs to be sequences, not arbitrary iterables.