Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'element': 0.07; 'remaining': 0.07; 'subject:two': 0.07; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'skip:[ 40': 0.16; 'skip:{ 40': 0.16; 'subject:based': 0.16; 'subject:key': 0.16; 'index': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'cc:addr:python.org': 0.22; '31,': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'subject:per': 0.31; 'subject:from': 0.34; 'could': 0.34; 'equal': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'adjust': 0.36; 'skip:[ 10': 0.38; 'pm,': 0.38; 'easy': 0.60; '2015': 0.84; 'convenience,': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Pdds1mWXBbe8ELfjQTmm0jNZHMikzOvZSFdRX1DCjWk=; b=gX/GM3jWBhR3nGymL/xw+UIxrBunLCHa+c7yk/A+bzv66gIlIFG3T8beD1S30d3R6c C8RKrnxVKrGZZgmvkChKFAg4sX+5knLAzQbJ09XLPvlVlXeVxAABm6R5ALMSshwnxL8N eu6lQjWmGm4mf74J4YsKsMM1ox8xW2hSFmYEVIppp34La7b7HAtVdPZgah+n3AsqODuF hhsmL0BzOcuFiroUVTz/uDkC1COcBXCjPawuvtD/06Ip7OOXQQEY77bIfxeUnRwZsNR/ hLou31wNAWYiRrkAf3sAinKqb7p9bS/MsBU0y3cSS44LgyrIeC1m119LFwFFZQop5f8E pHcw== MIME-Version: 1.0 X-Received: by 10.107.14.131 with SMTP id 125mr10902722ioo.53.1422671920162; Fri, 30 Jan 2015 18:38:40 -0800 (PST) In-Reply-To: <0dddee06-233b-436a-be48-3c16e62c1718@googlegroups.com> References: <0dddee06-233b-436a-be48-3c16e62c1718@googlegroups.com> Date: Sat, 31 Jan 2015 13:38:40 +1100 Subject: Re: Create dictionary based of x items per key from two lists From: Chris Angelico Cc: "python-list@python.org" 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422671928 news.xs4all.nl 2906 [2001:888:2000:d::a6]:58118 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84931 On Sat, Jan 31, 2015 at 1:27 PM, wrote: > 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 > } > > So last item from l2 is key for remaining items from l1 So the Nth element of l1 will always be paired with the (N/numL1PerL2)th element of l2 (with the check at the end)? Seems easy enough. dups = len(l1)/len(l2) l2.append(l2[-1]) result = {x:l2[i/dups] for i,x in enumerate(l1)} This mutates l2 for convenience, but you could also adjust the index to take care of the excess. As a one-liner: result = {x:l2[min(i/(len(l1)/len(l2)),len(l2)-1)] for i,x in enumerate(l1)} But the one-liner is not better code :) ChrisA