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
#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


Messages In This Thread
RE: Checking number in a list to see if they are beside each other - by perfringo - Aug-26-2019, 07:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,556 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,259 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Checking if a string contains all or any elements of a list k1llcod3 1 1,117 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,308 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,073 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,129 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Checking the number of arguments a function takes Chirumer 3 2,169 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Count number of occurrences of list items in list of tuples t4keheart 1 2,399 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 1,988 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