0

I am new to python and I am just trying to get a count of duplicate elements. My code looks something like this:

n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
  items=int(input("value for product " + str(i+1) + " : "))
    list1.append(items)
    
dup=[]

for a in list1:
    if list1.count(a)>1:
        dup.append(a)

print("Count of duplicate elements in the list: ",dup)

Output:

Enter the number of products in a list : 5
value for product 1 : 4
value for product 2 : 4
value for product 3 : 4
value for product 4 : 1
value for product 5 : 5
Count of duplicate elements in the list:  [4, 4, 4]

The answer should be 1 because it is just the number 4 that is a duplicate value but I get the above output. Can someone please advice?

2
  • 2
    There are many things to improve in your code, but just a quick fix, you could use len(set(dup)) in place of dup in your last print. Commented Oct 26, 2021 at 20:42
  • Can you elaborate on why you expected this code to result in 1 when you're literally printing the contents of a list? When would printing the contents of any list ever result in a single integer? Have you given it a try in modifying your logic to only add elements to dup if they aren't already present there? If you break down the issue you're seeing here into smaller requirements, they are all questions that have been asked here many times before. Can you include a summary of what research you've done, with an explanation as to why that falls short for your requirements? How to Ask Commented Oct 26, 2021 at 20:42

7 Answers 7

2

If the Count is greater than one, check if a is not in the dup list, then add a to it. Finally, print the length of the dup list

n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
    item = int(input("value for product " + str(i+1) + " : "))
    list1.append(item)
    
dup = []

for a in list1:
    if (list1.count(a) > 1) and (a not in dup):
        dup.append(a)

print("Count of duplicate elements in the list: ", len(dup))

Sign up to request clarification or add additional context in comments.

Comments

2

For what it's worth, here's a more advanced solution:

Generally for counting the occurrences of multiple elements, it's easiest to use a Counter. Once you have that, you just need to get the number of items that occur more than once.

list1 = [4, 4, 4, 1, 5]
# ---
from collections import Counter

counts = Counter(list1)  # > Counter({4: 3, 1: 1, 5: 1})
total = sum(count>1 for count in counts.values())

print(total)  # -> 1

This sum() works because count>1 returns False or True, and False == 0 and True == 1.

Comments

1

You're adding the duplicate to dup even if it's already in the list. You should check for that before adding it.

for a in list1:
    if list1.count(a)>1 and a not in dup:
        dup.append(a)

then if you want the count of duplicates, you should print the length of dup, not its contents.

print("Count of duplicate elements in the list: ",len(dup))

You could also make dup a set instead of a list. Then duplicates are ignored. Do this with the initialization:

dup = set()

and use .add() instead of .append().

Comments

1

An option could be to use a dict, so you then not only have the count of duplicates, but also what was duplicated and with how many occurrences.

n = int(input("Enter the number of products to be stored in a list : "))  # 

I have entered 5

list1 = []

for i in range(n):
    items = int(input("value for product " + str(i + 1) + " : "))
    list1.append(items)

dup = {}

for a in list1:
    if list1.count(a) > 1:
        dup[a] = dup.get(a, 0)+1

print("Count of duplicate elements in the list: ", len(dup))
print(dup)

Result is:

Enter the number of products to be stored in a list : 5
value for product 1 : 4
value for product 2 : 4
value for product 3 : 4
value for product 4 : 2
value for product 5 : 2
Count of duplicate elements in the list:  2
{4: 3, 2: 2}

Comments

0

With the numpy library, you could use numpy.unique to count every occurrence in your numpy array:

import numpy

for a in list1: 
    if list1.count(a)>1:
        dup.append(a)

dup_array = numpy.array(dup)
unique, counts = numpy.unique(dup, return_counts=True)
dict(zip(unique, counts))

2 Comments

You don't need NumPy to do this, the standard library has Counter, which I just posted an answer about.
@wjandrea I think it's a valid alternative anyway
0

It is simple. You can use Counter from collections to count the occurrences of each element in the list. Then you can count the elements which are not unique as follows.

from collections import Counter
n=int(input("Enter the number of products to be stored in a list : "))

list1=[]

for i in range(n):
    items=int(input("value for product " + str(i+1) + " : "))
    list1.append(items)
dup_dict=Counter(list1)
count=0
for i in dup_dict.values():
    if(i!=1):
        count+=1

print("Count of duplicate elements in the list: ",count)

Comments

0
`
input=list("AABBCCDDEE")
input1=set(input)
dict1=dict.fromkeys(input1,0)
print("Hello world")
for i in input:
    dict1[i]=dict1[i]+1
print(dict1)`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.