Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Howw to prevent the duplication of any value in a column within a CSV file (python) Date: Tue, 26 Apr 2016 14:19:20 -0600 Lines: 40 Message-ID: References: <75e5d8dc-e15b-4350-95e8-490abd4aa14f@googlegroups.com> <531e2dda-c522-4b11-a030-f3f37078f344@googlegroups.com> <49f4ffd8-d140-4f1b-95a9-75cb122e8b2f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Px+rotsRSaHo+Gb06HcFnQugRvq4c+t+3nXlwRR69FJg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'subject:file': 0.07; 'csv': 0.09; 'subject:CSV': 0.09; 'thread': 0.10; 'python': 0.10; 'subject:python': 0.14; 'subject: \n ': 0.15; '2016': 0.16; 'adam': 0.16; 'file.close()': 0.16; 'found"': 0.16; 'helps.': 0.16; 'ian.': 0.16; 'membership:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set()': 0.16; 'set:': 0.16; 'wrote:': 0.16; 'beginner': 0.18; 'example.': 0.18; 'noted': 0.22; 'header :In-Reply-To:1': 0.24; 'checking': 0.27; 'message- id:@mail.gmail.com': 0.27; '"no': 0.29; 'tutorial': 0.29; "i'm": 0.30; 'print': 0.30; 'url:python': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'membership': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'set.': 0.35; 'but': 0.36; 'list,': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:209': 0.38; 'subject:the': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'your': 0.60; 'hope': 0.61; 'skip:n 10': 0.62; 'elsewhere': 0.66; '26,': 0.72; 'subject:any': 0.84; 'subject:value': 0.84; 'to:name:python': 0.84; 'url:tutorial': 0.91 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; bh=AavGBMQTdpp37NdCKonb6KvdJo5pyPlyM8JZFb9smKU=; b=t5rvvzpDPT59irKKksB6bqSTS2BVOdkxP7c/TNIHOaSFyXo0BqhZ2ZG7X4yWJmuBFe lv2pWTbfNIjsrzKbmjR/K6aX4PBEfPS1/PK0OEeWwS2tSrRvjY0R9viSV2E/PDsrgd8q DGum4ua/NsUYL1tb2q7ivSA8tlEFt6mxf1L8lnNAdGQLZHJz3kXQNBc9cBNHSciq3wsF NzMd6xlmhLAbZAvYDPgnXG1asJZT3gwWs1U5f9gL/TdWw/XtuaK5dQKKGNqUjBOGLffM guOdxENXgwHK2pJXl7pKOz4gelvElPhmj+xVMcJjYpY1wVIe5X8oI2huoHg95+LgYCfU DbXg== 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:from:date :message-id:subject:to; bh=AavGBMQTdpp37NdCKonb6KvdJo5pyPlyM8JZFb9smKU=; b=K+4TwCnsgczJIXHFPuHp7xxwZ1n4zrWlOnUxWZi2nnHnwWJvPiCrKu5+b7499QFV2S iSz6tyXCAkIRfCwY8Ns+FQ30hAkzxAgeZL2Nu43w1OHsiGxVNMO38adO2k3GGAZJ/ByQ DQanIIBgbktz/tTxaMnB02sa17Eyj9xCCK0rZCjhjviq2iaKmVAocDgCbJUrLCvmzsiK sjDRzkN3ExRRVmmdivGkANZGgGf5EEWhuMEPIbrEa0z2yUyt2f4GHNw6BMTEnO6QKiJt /U4e/YHOETSVq/+j0qaq14axMQy98Z7ZEyEpvbksGoolnQ1nfSKKAN92rWE99fqVU59q 1krw== X-Gm-Message-State: AOPr4FWZ7TbRklwsEr8G1+7KK2ks51JqVIeIaSVcW6iywKHamM2a6qrrOxNalTA5qWyZGsT9otEqjiuuxp4W9w== X-Received: by 10.107.171.198 with SMTP id u189mr5977629ioe.111.1461701999386; Tue, 26 Apr 2016 13:19:59 -0700 (PDT) In-Reply-To: <49f4ffd8-d140-4f1b-95a9-75cb122e8b2f@googlegroups.com> 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: <75e5d8dc-e15b-4350-95e8-490abd4aa14f@googlegroups.com> <531e2dda-c522-4b11-a030-f3f37078f344@googlegroups.com> <49f4ffd8-d140-4f1b-95a9-75cb122e8b2f@googlegroups.com> Xref: csiph.com comp.lang.python:107687 On Tue, Apr 26, 2016 at 2:07 PM, Adam Davis wrote: > On Tuesday, 26 April 2016 20:52:54 UTC+1, Ian wrote: >> On Tue, Apr 26, 2016 at 1:05 PM, Joaquin Alzola >> wrote: >> > Just an example. Didn't use the csv but just hope that it helps. >> > >> > name=[] >> > name_exist="Dop" >> > >> > with open("dop.csv") as f: >> > for line in f: >> > line_split=line.split(',') >> > name.append(line_strip[0]) >> > if name_exist in name: >> > print "found name " + name_exist + " Can not append" >> > else: >> > file = open("dop.csv",'a') >> > print "No name found" >> > file.write(name_exist) >> > file.close() >> >> Except that as noted elsewhere in the thread a set would be a better >> choice than a list, since checking membership is much faster on a set. > > Thanks for your help Ian. I'm a beginner right now, could you instruct me as to how I would create a set? Create a new set: my_set = set() Add a value to the set: my_set.add(value) Check for membership: value in my_set See also the Python tutorial at https://docs.python.org/3/tutorial/datastructures.html#sets