Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB 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 21:23:32 +0100 Lines: 34 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; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de U7+gO9Yx7K/WvJBDzjUAiA19Ocry3SUip96/vhVvEE0A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:file': 0.07; 'csv': 0.09; 'subject:CSV': 0.09; 'thread': 0.10; 'subject:python': 0.14; 'subject: \n ': 0.15; '2016': 0.16; 'adam': 0.16; 'file.close()': 0.16; 'found"': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'helps.': 0.16; 'ian.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 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; 'header:User- Agent:1': 0.26; 'checking': 0.27; '"no': 0.29; "i'm": 0.30; 'print': 0.30; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'membership': 0.34; 'add': 0.34; 'could': 0.35; 'set.': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'faster': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'subject:the': 0.39; "didn't": 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; '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 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=q9EycRxEAAAA:8 a=dEy4AnFIG5xWnSMLpr4A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 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:107688 On 2016-04-26 21:07, 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 set: the_set = set() Add an item to a set: the_set.add(item)