Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two numbers in a list are the same

Tags:

python

I was curious on how I would check if 2 numbers inside of a list are identical. For instance,

myList=[1,7,9,3,1,2,8]

In this case, "1" is repeated in 'myList'.

How would I make a program that checks to see if two numbers inside of a list are the same(repeated). Try to use loops so I can understand because I haven't yet learned complex functions.

like image 218
Drake Walter Avatar asked Apr 22 '13 20:04

Drake Walter


People also ask

How do you check if there are same elements in a list?

Using Count() method A simple count of how many times an element occurs in the list. If its occurrence count is equal to the length of the list, then it means all elements in the list are the Same i.e.

How do you check if all values are the same in a list Python?

You can convert the list to a set. A set cannot have duplicates. So if all the elements in the original list are identical, the set will have just one element. if len(set(input_list)) == 1: # input_list has all identical elements.

How do you compare numbers in a list Python?

Using list.sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.


2 Answers

Using collections.Counter:

from collections import Counter

myList = [1, 7, 9, 3, 1, 2, 8]
counter = Counter(myList)
print(counter) # prints 'Counter({1: 2, 2: 1, 3: 1, 7: 1, 8: 1, 9: 1})'

With Collections.Counter, you know how many times is repeated every element of your source list myList.

After that you can use a simple list comprehension to know which elements are repeated:

result = [i for i, j in counter.items() if j > 1]
print(result) # prints '[1]'
like image 173
aldeb Avatar answered Oct 01 '22 12:10

aldeb


If you want to use loops, you'll have to use a list or a set of numbers which you've already seen. Then while looping you'll check, with the in operator if the number is already seen.

seen = []
for number in myList:
    if number in seen:
        print "Number repeated!"
    else:
        seen.append(number)

set does not allow duplicates in it, thus it's a good fit for this sort of an algorithm. As mentioned in the comments, the time complexity for checking if an element is in a set is constant for the average case (O(1)), so this is more efficient if you have a lot of numbers.

seen = set()
for number in myList:
    if number in seen:
        print "Number repeated!"
    seen.add(number) # won't duplicate

I'd say that the most pythonic way is to use collections.Counter, but the other answers cover this already. To use a built-in, you could generate a set of the numbers which appear more than once using a generator expression and set.

In [39]: seen = set()

In [40]: print list(set(x for x in myList if x in seen or seen.add(x)))
[1]

Here the expression will loop over all values in myList and add them to a set called seen if they have already been seen. Eventually, it will convert the resulting set into a list and print the contents.

like image 39
msvalkon Avatar answered Oct 01 '22 12:10

msvalkon