omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Find consecutive elements in a list

    Pythonista
    function consecutive list
    3
    3
    3827
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • craigmrock
      craigmrock last edited by

      Trying to return True if there are duplicate items in a list.
      Not working with numbers, and want to allow the user to make a list as long as they want...suggestions?

      user = input("What's on your list??: ")
      def has_consecutive():
      	items = user.split() #trying to split list into items to compare
      	if (items[0] == items[+1]): #trying to compare one item to the next to find duplicates
      		return True #return true if duplicates are found
      
      	else:
      		return False #return False if no duplicates are found 
      	
      	
      has_consecutive()```
      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @craigmrock last edited by cvp

        @craigmrock try this

        def has_duplicate():
        	user = '1 2 3 4 2'
        	items = user.split()
        	for i in range(0,len(items)-1): # loop on items except last one
        		if items[i] in items[i+1:]:		# check of i-th item exists in next ones
        			return True
        	return False
        print(has_duplicate())
        

        Checks if duplicates exist but not only consecutive...

        		if items[i] == items[i+1]:		# check if i-th item is equal to next one
        

        Will check consecutive duplicates

        1 Reply Last reply Reply Quote 0
        • enceladus
          enceladus last edited by enceladus

          def hasdupelems(l):
              return len(l) != len(set(l))
              
          print(hasdupelems(['ab', 'cd', 'ab', 'ef']))
          print(hasdupelems(['ab', 'cd',  'ef']))
          
          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Powered by NodeBB Forums | Contributors