UltraNoob
UltraNoob

Reputation: 17

How to find if there are 2 elements of same value consecutively in a list?

For example, [1,3,3,5] should return True while [1,3,1,3] should return False.

I am looking for a simple solution using loops.

I tried the following:

def conseq(nums):
    for i in range (len(nums)):
        if nums[i]==nums[i+1]:
            return True
            break
        else:
            return False

Upvotes: 1

Views: 702

Answers (7)

user3452643
user3452643

Reputation: 182

the return ends the function, so here it will stop the processing as well your true statement mostly works, and the break is unnecessary

you don't want the else statement until after the for loop ends (it returns False only if everything else has been parsed)

Also the way you parse through the code, nums can't access nums[i+1] when you're at the final nums so you need the range len(nums) - 1

If you feel like putting an else that does nothing, you can with a single semicolon or pass I believe, but the else is unnecessary here

def conseq(nums):
    for i in range (len(nums)-1):

        if nums[i]==nums[i+1]:
            return True
        else:
            ;
    return False

Upvotes: 2

JenilDave
JenilDave

Reputation: 604

We have many solutions here already. I have tried out using numpy without a loop:

>>> import numpy as np
>>> f = np.array([1,3,3,5])
>>> (np.where(np.diff(f) == 0)[0]).astype('bool')[0] #check if we have any difference of two elements 0?
True

Upvotes: 0

Robinhood
Robinhood

Reputation: 110

I modified your Code. Please check and let know if useful

def conseq(nums):
    flag=True
    for i in range (len(nums)-1):
        if nums[i]==nums[i+1]:
            print(True)
            flag=False
            break
        else:
            continue
    if(flag):
        print(False)
nums=[1,3,3,5]
nums1=[1,3,1,3]
conseq(nums)
conseq(nums1)

Upvotes: 0

programandoconro
programandoconro

Reputation: 2709

def conseq(nums):
    for i in range (len(nums)):
        if nums[i]==nums[i-1]:
            return True    
    return False


Upvotes: 0

  vrnvorona
vrnvorona

Reputation: 476

Enumerate list without last (to avoid try catch for i+1 being out of range) then compare each item with next one and return True if they are same. After loop return False because none are similar consequtively (returns break functions)

def function(number_list):
    for i, item in enumerate(number_list[:-1]):
        if item == number_list[i+1]:
            return True
    return False

Upvotes: 0

Omer Tuchfeld
Omer Tuchfeld

Reputation: 3022

The first time your function encounters 2 consecutive numbers which are different, it returns False. Returning from a function ends that function immediately, the function does not continue after that. This is also why the break is not necessary.

Another issue with your code is that once you reach the final number, nums[i + 1] will access out of the bounds of the array. That's why you should iterate over len(nums) - 1 rather than len(nums) - there's no reason to check the final number because there's nothing after it.

def conseq(nums):
    for i in range(len(nums) - 1):
        if nums[i]==nums[i+1]:
            return True

    # Only return False once we've exhausted all numbers.
    # Since we didn't return True so far - it means there are
    # no consecutive equal numbers, so we can safely return False
    return False

Upvotes: 3

Scott Hunter
Scott Hunter

Reputation: 49803

You can't return False until the loop is done (since the match may be in the part of the list you haven't checked yet). And the break after return will never happen (since return ends the function and everything in it).

Upvotes: 0

Related Questions