user1257255
user1257255

Reputation: 1171

Python: Compare more numbers

I would like to search for numbers in existing list. If is one of this numbers repeated then set variable's value to true and break for loop.

list = [3, 5, 3] //numbers in list

So if the function gets two same numbers then break for - in this case there is 3 repeated.

How to do that?

Upvotes: 1

Views: 253

Answers (5)

Nadir Sampaoli
Nadir Sampaoli

Reputation: 5555

You could look into sets. You loop through your list, and either add the number to a support set, or break out the loop.

>>> l = [3, 5, 3]
>>> s = set()
>>> s
set([])
>>> for x in l:
...     if x not in s:
...         s.add(x)
...     else:
...         break

You could also take a step further and make a function out of this code, returning the first duplicated number you find (or None if the list doesn't contain duplicates):

def get_first_duplicate(l):
    s = set()
    for x in l:
        if x not in s:
            s.add(x)
        else:
            return x

get_first_duplicate([3, 5, 3])
# returns 3

Otherwise, if you want to get a boolean answer to the question "does this list contain duplicates?", you can return it instead of the duplicate element:

def has_duplicates(l):
    s = set()
    for x in l:
        if x not in s:
            s.add(x)
        else:
            return true
    return false

get_first_duplicate([3, 5, 3])
# returns True

senderle pointed out:

there's an idiom that people sometimes use to compress this logic into a couple of lines. I don't necessarily recommend it, but it's worth knowing:

s = set(); has_dupe = any(x in s or s.add(x) for x in l)

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251156

you can use collections.Counter() and any():

>>> lis=[3,5,3]
>>> c=Counter(lis)
>>> any(x>1 for x in c.values()) # True means yes some value is repeated
True
>>> lis=range(10)
>>> c=Counter(lis)
>>> any(x>1 for x in c.values()) # False means all values only appeared once
False

or use sets and match lengths:

In [5]: lis=[3,3,5]

In [6]: not (len(lis)==len(set(lis)))
Out[6]: True

In [7]: lis=range(10)

In [8]: not (len(lis)==len(set(lis)))
Out[8]: False

Upvotes: 2

adray
adray

Reputation: 1448

without additional memory:

any(l.count(x) > 1 for x in l)

Upvotes: 0

Junuxx
Junuxx

Reputation: 14281

First, don't name your list list. That is a Python built-in, and using it as a variable name can give undesired side effects. Let's call it L instead.

You can solve your problem by comparing the list to a set version of itself.

Edit: You want true when there is a repeat, not the other way around. Code edited.

def testlist(L):
    return sorted(set(L)) != sorted(L)

Upvotes: 4

schesis
schesis

Reputation: 59238

You should never give the name list to a variable - list is a type in Python, and you can give yourself all kinds of problems masking built-in names like that. Give it a descriptive name, like numbers.

That said ... you could use a set to keep track of which numbers you've already seen:

def first_double(seq):
    """Return the first item in seq that appears twice."""
    found = set()
    for item in seq:
        if item in found:
            return item
            # return will terminate the function, so no need for 'break'.
        else:
            found.add(item)

numbers = [3, 5, 3]
number = first_double(numbers)

Upvotes: 0

Related Questions