<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi,<br><br>You can use method name count in list also for getting the repeats.<br><br>#!/usr/bin/python<br><br>li=[1,2,3,1,4,5,7,3,4,1]<br>for i in range(len(li)):<br>&nbsp;&nbsp;&nbsp; print li[i], li.count(li[i])<br><br>cheers,<br><br>Saurabh<br><br><br><br>--- On <b>Fri, 11/6/10, tutor-request@python.org <i>&lt;tutor-request@python.org&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: tutor-request@python.org &lt;tutor-request@python.org&gt;<br>Subject: Tutor Digest, Vol 76, Issue 31<br>To: tutor@python.org<br>Date: Friday, 11 June, 2010, 8:19 PM<br><br><div class="plainMail">Send Tutor mailing list submissions to<br>&nbsp;&nbsp;&nbsp; <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br><br>To subscribe or unsubscribe via the World
 Wide Web, visit<br>&nbsp;&nbsp;&nbsp; <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>or, via email, send a message with subject or body 'help' to<br>&nbsp;&nbsp;&nbsp; <a ymailto="mailto:tutor-request@python.org" href="/mc/compose?to=tutor-request@python.org">tutor-request@python.org</a><br><br>You can reach the person managing the list at<br>&nbsp;&nbsp;&nbsp; <a ymailto="mailto:tutor-owner@python.org" href="/mc/compose?to=tutor-owner@python.org">tutor-owner@python.org</a><br><br>When replying, please edit your Subject line so it is more specific<br>than "Re: Contents of Tutor digest..."<br><br><br>Today's Topics:<br><br>&nbsp;&nbsp;&nbsp;1. Looking for duplicates within a list (Ken G.)<br>&nbsp;&nbsp;&nbsp;2. Re: Looking for duplicates within a list (Alex Hall)<br>&nbsp;&nbsp;&nbsp;3. Re: Looking for duplicates within a list (Ken G.)<br>&nbsp;&nbsp;&nbsp;4. Re: Looking for
 duplicates within a list (Ken G.)<br>&nbsp;&nbsp;&nbsp;5. Re: Looking for duplicates within a list (Sander Sweers)<br>&nbsp;&nbsp;&nbsp;6. Re: Looking for duplicates within a list (Jose Amoreira)<br><br><br>----------------------------------------------------------------------<br><br>Message: 1<br>Date: Fri, 11 Jun 2010 09:57:34 -0400<br>From: "Ken G." &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt;<br>To: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: [Tutor] Looking for duplicates within a list<br>Message-ID: &lt;<a ymailto="mailto:4C1240CE.7050807@insightbb.com" href="/mc/compose?to=4C1240CE.7050807@insightbb.com">4C1240CE.7050807@insightbb.com</a>&gt;<br>Content-Type: text/plain; charset=ISO-8859-1; format=flowed<br><br>I have been working on this problem for several days and I am not making <br>any progress.&nbsp;
 I have a group of 18 number, in ascending order, within a <br>list.&nbsp; They ranged from 1 to 39.&nbsp; Some numbers are duplicated as much as <br>three times or as few as none.<br><br>I started with one list containing the numbers.&nbsp; For example, they are <br>listed as like below:<br><br>a = [1, 2, 3, 3, 4]<br><br>I started off with using a loop:<br><br>&nbsp; &nbsp; for j in range (0, 5):<br>&nbsp; &nbsp; x = a[0] # for example, 1<br><br>How would I compare '1' with 2, 3, 3, 4? <br><br>Do I need another duplicated list such as b = a and compare a[0] with <br>either b[0], b[1], b[2], b[3], b[4]?<br><br>Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br><br>In any event, if a number is listed more than once, I would like to know <br>how many times, such as 2 or 3 times.&nbsp; For example, '3' is listed twice <br>within a list.<br><br>TIA,<br><br>Ken<br><br><br><br><br><br><br><br><br><br><br>------------------------------<br><br>Message:
 2<br>Date: Fri, 11 Jun 2010 10:20:20 -0400<br>From: Alex Hall &lt;<a ymailto="mailto:mehgcap@gmail.com" href="/mc/compose?to=mehgcap@gmail.com">mehgcap@gmail.com</a>&gt;<br>To: "Ken G." &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt;<br>Cc: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID:<br>&nbsp;&nbsp;&nbsp; &lt;<a ymailto="mailto:AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681@mail.gmail.com" href="/mc/compose?to=AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681@mail.gmail.com">AANLkTilR4cZMiC1tsAMuTuzO8C32SO-qKDXe9spco681@mail.gmail.com</a>&gt;<br>Content-Type: text/plain; charset=ISO-8859-1<br><br>On 6/11/10, Ken G. &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt; wrote:<br>&gt; I have
 been working on this problem for several days and I am not making<br>&gt; any progress.&nbsp; I have a group of 18 number, in ascending order, within a<br>&gt; list.&nbsp; They ranged from 1 to 39.&nbsp; Some numbers are duplicated as much as<br>&gt; three times or as few as none.<br>FYI, Python's "set" data type will let you have a list and never have<br>a repeat. I know that is not your goal now, but if you want to remove<br>duplicates, it seems like a good choice.<br>&gt;<br>&gt; I started with one list containing the numbers.&nbsp; For example, they are<br>&gt; listed as like below:<br>&gt;<br>&gt; a = [1, 2, 3, 3, 4]<br>&gt;<br>&gt; I started off with using a loop:<br>&gt;<br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;for j in range (0, 5):<br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;x = a[0] # for example, 1<br>&gt;<br>&gt; How would I compare '1' with 2, 3, 3, 4?<br>&gt;<br>&gt; Do I need another duplicated list such as b = a and compare a[0] with<br>&gt; either b[0],
 b[1], b[2], b[3], b[4]?<br>&gt;<br>&gt; Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br>A couple points here. First, you will want to make life easier by<br>saying range(0, len(a)) so that the loop will work no matter the size<br>of a.<br>Second, for comparing a list to itself, here is a rather inefficient,<br>though simple, way:<br><br>for i in range(0, len(a)):<br> x=a[i]<br> for j in range(0, len(a)):<br>&nbsp; y=a[j]<br>&nbsp; if(x==y and i!=j): #match since a[i]==a[j] and i and j are not the<br>same index of a<br>&gt;<br>&gt; In any event, if a number is listed more than once, I would like to know<br>&gt; how many times, such as 2 or 3 times.&nbsp; For example, '3' is listed twice<br>&gt; within a list.<br>Do not quote me here, but I think sets may be able to tell you that as well.<br>&gt;<br>&gt; TIA,<br>&gt;<br>&gt; Ken<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt;
 _______________________________________________<br>&gt; Tutor maillist&nbsp; -&nbsp; <a ymailto="mailto:Tutor@python.org" href="/mc/compose?to=Tutor@python.org">Tutor@python.org</a><br>&gt; To unsubscribe or change subscription options:<br>&gt; <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>&gt;<br><br><br>-- <br>Have a great day,<br>Alex (msg sent from GMail website)<br><a ymailto="mailto:mehgcap@gmail.com" href="/mc/compose?to=mehgcap@gmail.com">mehgcap@gmail.com</a>; <a href="http://www.facebook.com/mehgcap" target="_blank">http://www.facebook.com/mehgcap</a><br><br><br>------------------------------<br><br>Message: 3<br>Date: Fri, 11 Jun 2010 10:31:58 -0400<br>From: "Ken G." &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt;<br>To: <a ymailto="mailto:vnbang2003@yahoo.com"
 href="/mc/compose?to=vnbang2003@yahoo.com">vnbang2003@yahoo.com</a>, <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID: &lt;<a ymailto="mailto:4C1248DE.6090508@insightbb.com" href="/mc/compose?to=4C1248DE.6090508@insightbb.com">4C1248DE.6090508@insightbb.com</a>&gt;<br>Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"<br><br>vijay wrote:<br>&gt; Check out this code<br>&gt;&nbsp; l= [1, 2, 3, 3, 4]<br>&gt;&nbsp; d={}<br>&gt;&nbsp; for item in l:<br>&gt;&nbsp; &nbsp; &nbsp; d.setdefaut(item,0)<br>&gt;&nbsp; &nbsp; &nbsp; d[item] +=1<br>&gt; print d<br>&gt; {1: 1, 2: 1, 3: 2, 4: 1}<br>&gt;<br>&gt;<br>&gt; with regard's<br>&gt; vijay<br>&gt;<br>&gt;<br>Thanks.&nbsp; Very interesting concept.<br><br>Ken<br>-------------- next part --------------<br>An HTML attachment was scrubbed...<br>URL: &lt;<a
 href="http://mail.python.org/pipermail/tutor/attachments/20100611/12e042f9/attachment-0001.html" target="_blank">http://mail.python.org/pipermail/tutor/attachments/20100611/12e042f9/attachment-0001.html</a>&gt;<br><br>------------------------------<br><br>Message: 4<br>Date: Fri, 11 Jun 2010 10:40:58 -0400<br>From: "Ken G." &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt;<br>To: Alex Hall &lt;<a ymailto="mailto:mehgcap@gmail.com" href="/mc/compose?to=mehgcap@gmail.com">mehgcap@gmail.com</a>&gt;<br>Cc: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID: &lt;<a ymailto="mailto:4C124AFA.7030007@insightbb.com" href="/mc/compose?to=4C124AFA.7030007@insightbb.com">4C124AFA.7030007@insightbb.com</a>&gt;<br>Content-Type: text/plain; charset="iso-8859-1";
 Format="flowed"<br><br>Alex Hall wrote:<br>&gt; On 6/11/10, Ken G. &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt; wrote:<br>&gt;&nbsp;&nbsp;&nbsp;<br>&gt;&gt; I have been working on this problem for several days and I am not making<br>&gt;&gt; any progress.&nbsp; I have a group of 18 number, in ascending order, within a<br>&gt;&gt; list.&nbsp; They ranged from 1 to 39.&nbsp; Some numbers are duplicated as much as<br>&gt;&gt; three times or as few as none.<br>&gt;&gt;&nbsp; &nbsp;&nbsp;&nbsp;<br>&gt; FYI, Python's "set" data type will let you have a list and never have<br>&gt; a repeat. I know that is not your goal now, but if you want to remove<br>&gt; duplicates, it seems like a good choice.<br>&gt;&nbsp;&nbsp;&nbsp;<br>&gt;&gt; I started with one list containing the numbers.&nbsp; For example, they are<br>&gt;&gt; listed as like below:<br>&gt;&gt;<br>&gt;&gt; a = [1, 2, 3, 3,
 4]<br>&gt;&gt;<br>&gt;&gt; I started off with using a loop:<br>&gt;&gt;<br>&gt;&gt;&nbsp; &nbsp;&nbsp;&nbsp;for j in range (0, 5):<br>&gt;&gt;&nbsp; &nbsp;&nbsp;&nbsp;x = a[0] # for example, 1<br>&gt;&gt;<br>&gt;&gt; How would I compare '1' with 2, 3, 3, 4?<br>&gt;&gt;<br>&gt;&gt; Do I need another duplicated list such as b = a and compare a[0] with<br>&gt;&gt; either b[0], b[1], b[2], b[3], b[4]?<br>&gt;&gt;<br>&gt;&gt; Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br>&gt;&gt;&nbsp; &nbsp;&nbsp;&nbsp;<br>&gt; A couple points here. First, you will want to make life easier by<br>&gt; saying range(0, len(a)) so that the loop will work no matter the size<br>&gt; of a.<br>&gt; Second, for comparing a list to itself, here is a rather inefficient,<br>&gt; though simple, way:<br>&gt;<br>&gt; for i in range(0, len(a)):<br>&gt;&nbsp; x=a[i]<br>&gt;&nbsp; for j in range(0, len(a)):<br>&gt;&nbsp;&nbsp;&nbsp;y=a[j]<br>&gt;&nbsp;&nbsp;&nbsp;if(x==y and i!=j):
 #match since a[i]==a[j] and i and j are not the<br>&gt; same index of a<br>&gt;&nbsp;&nbsp;&nbsp;<br>&gt;&gt; In any event, if a number is listed more than once, I would like to know<br>&gt;&gt; how many times, such as 2 or 3 times.&nbsp; For example, '3' is listed twice<br>&gt;&gt; within a list.<br>&gt;&gt;&nbsp; &nbsp;&nbsp;&nbsp;<br>&gt; Do not quote me here, but I think sets may be able to tell you that as well.<br>&gt;&nbsp;&nbsp;&nbsp;<br>&gt;&gt; TIA,<br>&gt;&gt;<br>&gt;&gt; Ken<br>&gt;&gt;&nbsp; &nbsp;&nbsp;&nbsp;<br>Thank you for your contribution.&nbsp; As seen here, I have much to learn.<br><br>Ken<br><br>-------------- next part --------------<br>An HTML attachment was scrubbed...<br>URL: &lt;<a href="http://mail.python.org/pipermail/tutor/attachments/20100611/eacd401a/attachment-0001.html"
 target="_blank">http://mail.python.org/pipermail/tutor/attachments/20100611/eacd401a/attachment-0001.html</a>&gt;<br><br>------------------------------<br><br>Message: 5<br>Date: Fri, 11 Jun 2010 16:46:25 +0200<br>From: Sander Sweers &lt;<a ymailto="mailto:sander.sweers@gmail.com" href="/mc/compose?to=sander.sweers@gmail.com">sander.sweers@gmail.com</a>&gt;<br>To: "Ken G." &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt;<br>Cc: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID:<br>&nbsp;&nbsp;&nbsp; &lt;<a ymailto="mailto:AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4@mail.gmail.com" href="/mc/compose?to=AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4@mail.gmail.com">AANLkTikGt6zifb0gOCSOR5vnvxg2MtFdaWJjr5RHBrb4@mail.gmail.com</a>&gt;<br>Content-Type: text/plain;
 charset=UTF-8<br><br>On 11 June 2010 15:57, Ken G. &lt;<a ymailto="mailto:beachkid@insightbb.com" href="/mc/compose?to=beachkid@insightbb.com">beachkid@insightbb.com</a>&gt; wrote:<br>&gt; In any event, if a number is listed more than once, I would like to know how<br>&gt; many times, such as 2 or 3 times. ?For example, '3' is listed twice within a<br>&gt; list.<br><br>If you do not have top keep the order of the number this will work.<br><br>&gt;&gt;&gt; a = [1, 2, 3, 3, 4]<br>&gt;&gt;&gt; counted = {}<br>&gt;&gt;&gt; for n in a:<br>&nbsp;&nbsp;&nbsp; if not n in counted:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; counted[n] = 1<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; counted[n] += 1<br><br>&gt;&gt;&gt; counted<br>{1: 1, 2: 1, 3: 2, 4: 1}<br><br>&gt;&gt;&gt; for x, y in counted.items():<br>&nbsp;&nbsp;&nbsp; if y &gt; 1:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "Number %s was found %s times" % (x,
 y)<br>&nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "Number %s was found %s time" % (x, y)<br><br>Number 1 was found 1 time<br>Number 2 was found 1 time<br>Number 3 was found 2 times<br>Number 4 was found 1 time<br><br>Greets<br>Sander<br><br><br>------------------------------<br><br>Message: 6<br>Date: Fri, 11 Jun 2010 15:49:22 +0100<br>From: Jose Amoreira &lt;<a ymailto="mailto:ljmamoreira@gmail.com" href="/mc/compose?to=ljmamoreira@gmail.com">ljmamoreira@gmail.com</a>&gt;<br>To: <a ymailto="mailto:tutor@python.org" href="/mc/compose?to=tutor@python.org">tutor@python.org</a><br>Subject: Re: [Tutor] Looking for duplicates within a list<br>Message-ID: &lt;<a ymailto="mailto:201006111549.22751.ljmamoreira@gmail.com" href="/mc/compose?to=201006111549.22751.ljmamoreira@gmail.com">201006111549.22751.ljmamoreira@gmail.com</a>&gt;<br>Content-Type: Text/Plain;&nbsp; charset="iso-8859-1"<br><br>On Friday, June 11, 2010 02:57:34 pm Ken
 G. wrote:<br>&gt; I have been working on this problem for several days and I am not making<br>&gt; any progress.&nbsp; I have a group of 18 number, in ascending order, within a<br>&gt; list.&nbsp; They ranged from 1 to 39.&nbsp; Some numbers are duplicated as much as<br>&gt; three times or as few as none.<br>&gt; <br>&gt; I started with one list containing the numbers.&nbsp; For example, they are<br>&gt; listed as like below:<br>&gt; <br>&gt; a = [1, 2, 3, 3, 4]<br>&gt; <br>&gt; I started off with using a loop:<br>&gt; <br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;for j in range (0, 5):<br>&gt;&nbsp; &nbsp;&nbsp;&nbsp;x = a[0] # for example, 1<br>&gt; <br>&gt; How would I compare '1' with 2, 3, 3, 4?<br>&gt; <br>&gt; Do I need another duplicated list such as b = a and compare a[0] with<br>&gt; either b[0], b[1], b[2], b[3], b[4]?<br>&gt; <br>&gt; Or do I compare a[0] with a[1], a[2], a[3], a[4]?<br>&gt; <br>&gt; In any event, if a number is listed more than once, I
 would like to know<br>&gt; how many times, such as 2 or 3 times.&nbsp; For example, '3' is listed twice<br>&gt; within a list.<br>&gt; <br>&gt; TIA,<br>&gt;<br><br>I would do it with a dictionary:<br>def reps(lst):<br>&nbsp;&nbsp;&nbsp; dict = {}<br>&nbsp;&nbsp;&nbsp; for item in lst:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if item in dict:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dict[item] += 1<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dict[item] = 1<br>&nbsp;&nbsp;&nbsp; return dict<br><br>This function returns a dictionary with of the number of times each value in <br>the list is repeated. Even shorter using dict.setdefault:<br><br>def reps(lst):<br>&nbsp;&nbsp;&nbsp; dict={}<br>&nbsp;&nbsp;&nbsp; for item in lst:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dict[item] = dict.setdefault(item,0) + 1<br>&nbsp;&nbsp;&nbsp; return dict<br><br>For instance, if
 lst=[1,2,2,2,4,4,5], then reps(lst) returns<br>{1: 1, 2: 3, 4: 2, 5: 1}<br><br>Using the fact that the list is ordered, one can design a more efficient <br>solution (go through the list; if this item is equal to the previous, then it <br>is repeated, else, it is a new value). But you list is short enough for this <br>direct approach to work.<br>Hope this helps. Cheers,<br>Jose<br><br><br>------------------------------<br><br>_______________________________________________<br>Tutor maillist&nbsp; -&nbsp; <a ymailto="mailto:Tutor@python.org" href="/mc/compose?to=Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br><br><br>End of Tutor Digest, Vol 76, Issue 31<br>*************************************<br></div></blockquote></td></tr></table><br>