Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: repeat items in a list Date: Sat, 26 Mar 2016 23:28:59 +0000 Lines: 35 Message-ID: References: <8935d5dc-5e62-4fa8-8e8f-bd5b1787ee9f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de g/MBJ1xcMxWmYS7V6f+d0AOUnVy17NdFxKQTicXGYQVg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'repeated': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'suggest': 0.15; "'a',": 0.16; "'b',": 0.16; "['a',": 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'stdlib.': 0.16; 'wrote:': 0.16; 'language': 0.19; 'lawrence': 0.22; 'elements': 0.23; 'header:In- Reply-To:1': 0.24; 'sort': 0.25; 'module': 0.25; 'header:User- Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'print': 0.30; 'another': 0.32; 'language.': 0.32; 'url:python': 0.33; 'list': 0.34; 'ones': 0.35; 'follows:': 0.35; 'asking': 0.35; 'there': 0.36; 'url:org': 0.36; 'structures': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'building': 0.38; 'to:addr:python.org': 0.40; 'mark': 0.40; 'url:3': 0.60; 'charset:windows-1252': 0.62; 'great': 0.63; 'our': 0.64; 'python- list': 0.66; 'today.': 0.67; 'swiss': 0.72; 'learn.': 0.84; 'pythonistas,': 0.84; 'army': 0.95 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.171.250 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: <8935d5dc-5e62-4fa8-8e8f-bd5b1787ee9f@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105796 On 26/03/2016 22:12, beliavsky--- via Python-list wrote: > I can create a list that has repeated elements of another list as follows: > > xx = ["a","b"] > nrep = 3 > print xx > yy = [] > for aa in xx: > for i in range(nrep): > yy.append(aa) > print yy > > output: > ['a', 'b'] > ['a', 'a', 'a', 'b', 'b', 'b'] > > Is there a one-liner to create a list with repeated elements? > yy = [aa for aa in xx for _ in range(nrep)] I suggest that you try this sort of the thing at an interactive prompt, it's a great way to learn. You might also want to take a look at the itertools module https://docs.python.org/3/library/itertools.html. This is often used in building structures like the ones you've been asking about today. To me it is the Swiss Army Knife of the stdlib. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence