Python Forum
Checking number in a list to see if they are beside each other
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking number in a list to see if they are beside each other
#1
List1 =[2,3,4,9,9,5,1]
def checkList(List1):
    for i in range(len(List1 - 1)):
      
if List1[i] == 9 and List1[i+1] == 9:
            return True
          return false 
i want to check if the number 9 comes b4 any other number in the list but my problem is the code does not return true or false
Reply
#2
List1 =[2,3,4,9,9,5,1]
lowercase for variable names.


for i in range(len(List1 - 1)):
-1 should be taken from the result of calling len(List1)
Don't use range to loop over items, loop over them directly.


Indentation is wrong


return false
Should be capital F False


A better way

function check_list that takes in list as it parameter
  • store the first item of the list in a variable by indexing the list.
  • loop through the items of the list starting at the second item by slicing the list.
    • check if the looped item equals 9 and the previous item equals 9.
      • return True.
    • set the previous item to the looped item.
  • return False
Reply
#3
number = [2, 3, 4, 9, 8, 5, 1]

def checkList(List1):
    for i in range(len(List1) - 1):  #dont understand what you want me to do here 
        if List1[i] == 9 and List1[i + 1] == 9:
            return True
    return False

checkList(number)
i dont get any error when i run the code but i also dont get any output
Reply
#4
You don't print the result and the list does't have any 9 next to each other so it will return False.

See Never use "for i in range(len(sequence)):"
Reply
#5
(Aug-25-2019, 10:13 PM)Allaye Wrote:
number = [2, 3, 4, 9, 8, 5, 1] def checkList(List1): for i in range(len(List1) - 1): #dont understand what you want me to do here if List1[i] == 9 and List1[i + 1] == 9: return True return False checkList(number)
i dont get any error when i run the code but i also dont get any output

You've to print the output instead if returning it, to see the output
Reply
#6
I suggest to follow naming conventions set in PEP8 - Function and Variable Names.

Alternative approach could be using zip, set method .difference and built-in function any. This way in process of writing code one can learn much more of Python Smile .

zip is for creating pairs of consecutive numbers without need to worry about IndexError:

>>> nums = [1, 9, 9, 2]
>>> list(zip(nums, nums[1:]))
[(1, 9), (9, 9), (9, 2)]
set method .difference can be used for determining whether elements in pair are equal and match desired value:

>>> set((1, 9)).difference((9,))
{1}
>>> set((9, 9)).difference((9,))
set()
In Python empty means False, so to get True if there is empty set one will use 'not'.

any() is built-in function with short circuiting behaviour.

Therefore function could be written (keyword argument is for cases when we need check numbers other than 9):

>>> def check_list(nums, sentinel=9):
...     return any(not set(pair).difference((sentinel,)) for pair in zip(nums, nums[1:]))
...
>>> check_list([1, 2, 3])
False
>>> check_list([1, 9, 9])
True
>>> check_list([1, 2, 2], sentinel=2)
True
One can use built-in help in interactive interpreter to learn about functions and methods used above (help(zip), help(set.difference), help(any)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,926 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 2,980 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Checking the number of arguments a function takes Chirumer 3 2,113 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Count number of occurrences of list items in list of tuples t4keheart 1 2,343 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 1,927 Oct-28-2020, 05:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020