Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: justin walters Newsgroups: comp.lang.python Subject: Re: Best way to clean up list items? Date: Mon, 2 May 2016 10:10:02 -0700 Lines: 24 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de SS5vuQ5JQXVwAO9wESNb/gDf1ODNoo7aVWECKlOTeBfw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'skip:[ 20': 0.03; "','": 0.16; '2016': 0.16; '\\r\\n': 0.16; 'dfs': 0.16; 'list1': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'have:': 0.18; '>': 0.18; 'fine,': 0.22; 'item.': 0.22; 'am,': 0.23; 'wrote': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'whitespace': 0.29; 'skip:[ 10': 0.31; 'compiled': 0.32; 'maybe': 0.33; '"")': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'replace': 0.35; 'item': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; '2\xc2\xa0': 0.84; 'want:': 0.84; 'subject:Best': 0.93 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:to; bh=1OAJk5+8qgaRo67I7JfoLwpEn0f7pW3E+KjLUkePCkg=; b=dM1Xb6SCoW2iZ3fOt9YUitjRCScSedA6bAivoinKShHQ1VrkSndxLKNIlFQS4BHuIz LvKpLILlHovUf/dGJwXsEgZsAtPU/UAxhwWyf0lEanoezMGbpExJvzsU0cQkT1vx9l1N GExUw5dyBDkotChDUuzznN9TVqe9voouMaH8pexNx9FEg9uuQ4Q5q3Qku1/vxAvOeuu+ /3efhJ/xx5Jx23v/m/HnYUq8MTJJu90x+/+9diETHEwmvWqeRGoAQd1UsNHcwPz8LmhF dZf3R0WXHrf4At3/K17SboTKnEoU6kp4ETesKtOgyZr/MXofBMPKCMcg7WSoI+z3e3E4 V/BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=1OAJk5+8qgaRo67I7JfoLwpEn0f7pW3E+KjLUkePCkg=; b=EMVmq68i7ykpSenh3gjYI2PTYc7haCwVGK+pQzdp2YwZY66Y3znXuwRhD2M9ON5MFT BK6ZoT4auCo1GbWvrCl6fjG5UqKMAKcT7WXiA2t0RXvVyYMbzesjHjO78nTgfDyCbQl5 5DtBxub6dOph5GSDWR5qx/ko/or5Nyqpq5nFIME3fnANCa4Oww9JffIIoaEgeALoQkZM MaWEjVCSohgx7+dqp7jj8PSpZjauUU8oIlMNUT5h1Le9x3q4YG37qBkr50eaVoFWkEZ/ eOv9AiQ29GIq7Z0RNbABu7EurlvmO1MPOnYFCuO5cV1INMU+4MnIMBl0CLNKM30PJKp2 ZNMA== X-Gm-Message-State: AOPr4FXMz8EyIgGBLxpfIKnwJVe5j5vC1xt3+eM5PTYt3FCaQ7aIY3k5VON7fBY8qv8W7WGLYZyxXiZjriExrw== X-Received: by 10.112.172.199 with SMTP id be7mr15375295lbc.49.1462209002964; Mon, 02 May 2016 10:10:02 -0700 (PDT) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.22 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:108021 On May 2, 2016 10:03 AM, "Jussi Piitulainen" wrote: > > DFS writes: > > > Have: list1 = ['\r\n Item 1 ',' Item 2 ','\r\n '] > > Want: list1 = ['Item 1','Item 2'] > > > > > > I wrote this, which works fine, but maybe it can be tidier? > > > > 1. list2 = [t.replace("\r\n", "") for t in list1] #remove \r\n > > 2. list3 = [t.strip(' ') for t in list2] #trim whitespace > > 3. list1 = filter(None, list3) #remove empty items > > > > After each step: > > > > 1. list2 = [' Item 1 ',' Item 2 ',' '] #remove \r\n > > 2. list3 = ['Item 1','Item 2',''] #trim whitespace > > 3. list1 = ['Item 1','Item 2'] #remove empty items You could also try compiled regex to remove unwanted characters. Then loop through the list and do a replace for each item.