0

Sorry if this is a simple question. I am new to Python. I am trying to write a function that will detect if there are 2 consecutive 2's next to each other. nums is a random array of ints. However, I'm getting an error that says list out of range. Can anyone tell me why this is? Thanks!

def has22(nums):
 for ii in nums:
    if nums[ii]==2:
      if ii+1 < len(nums):
        if ii+1 == 2:
          return True
  return False
Vivek Reddy
  • 92
  • 1
  • 1
  • 9
  • 1
    If your list has 5 items in it, but the _value_ of one of those items is, for example, the number 9, then `nums[ii]` will look for the 10th item in that list (10th due to zero indexing). – roganjosh Jul 18 '17 at 20:08
  • 2
    Note: `ii` will be the number itself, not the index into your 'array'. – Martin Evans Jul 18 '17 at 20:10
  • 1
    I'd use a variation of Kasramvd's 2nd version in the linked question: `any(i == j == 2 for i, j in zip(nums, nums[1:]))`. – PM 2Ring Jul 18 '17 at 20:23

4 Answers4

3

As previous commenters have pointed out, ii is the actual number instead of the index of the number in the list. For example, with the list nums = [3, 7, 4], the loop uses 3, 7, and 4 as ii, not 0, 1, and 2. This would cause an error because each of those numbers are larger than the maximum index.

One way to solve this (arguably more elegant than the other suggestions) is to use enumerate()

def has22(nums):
  for index, num in enumerate(nums):
    if num==2:
      if index+1 < len(nums):
        if nums[index+1] == 2:
          return True
  return False

enumerate() returns an iterator, which for each item returns a tuple containing the index of the item and the item itself. Thus, index, num are assigned to the number's index in the list and the number itself, respectively.

jmcampbell
  • 378
  • 4
  • 17
  • But even then these approaches are very reminiscent of naive language approaches. I believe using regex for each element will be much better like using MATCH, etc. – anugrah Jul 18 '17 at 20:19
  • 2
    @anugrah No! Using regex for this is overkill, and besides, you'd have to convert the list items to a string to use regex, – PM 2Ring Jul 18 '17 at 20:20
1

Please see the comment below

def has22(nums):
    for i in range(len(nums)-1): # here we get the length of list - 1
       if nums[i]==2 and nums[i+1]==2: # in this if condition, we check whether 2 consecutive 2 exist or not: yes ->return true
           return True
    return False # if no 2 consecutive 2 found in the list, return false
Mr_U4913
  • 1,294
  • 8
  • 12
1
def has22(nums):
 for ii in range(len(nums)):
    if nums[ii]==2:
      if ii+1 < len(nums):
        if nums[ii+1] == 2:
          return True
 return False

I would recommend doing something more elegant though, like this

def has22(nums):
    for i in range(len(nums)-1):
        if nums[i] == nums[i+1] == 2:
            return True
    return False
jacoblaw
  • 1,263
  • 9
  • 9
0

Change:

for ii in nums:

to

for ii in range(len(nums) - 1):

and

if ii+1 == 2:

to

if nums[ii+1] == 2: