Python Forum
getting previous/next valid value in a list, cyclically
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting previous/next valid value in a list, cyclically
#1
I have a list of values... some are valid, some invalid (containing None). I test each element for validity. When I find an invalid element, I need to find indexes of the closest previous and the closest next valid value. I need to treat the list as a cycle, so if there are no valid elements before/after the initial element, it should search from the end/start of the list... This way, if there is at least one valid value in the list, both the previous and the next valid value will be found (it can be the same index, if there is only one valid value in the list).

I am able to solve it, but with a ridiculously complicated code. Could you help me find some elegant solution? I am not a programmer, just using my very humble programming skills to process data in another field.
Reply


Messages In This Thread
getting previous/next valid value in a list, cyclically - by studeo - Mar-19-2018, 03:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  is this a valid variable name? Skaperen 6 1,634 Jun-05-2024, 10:13 PM
Last Post: Skaperen
  Script getting reindexing only valid error cubangt 1 3,356 Dec-07-2023, 04:06 PM
Last Post: cubangt
Question Use function, retry until valid Ashcora 8 3,318 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  checking for valid hexadecimal digits Skaperen 3 10,414 Sep-02-2021, 07:22 AM
Last Post: buran
  How to verify the give number is valid Mekala 3 3,176 May-16-2020, 02:40 PM
Last Post: anbu23
  is a str object a valid iterator? Skaperen 6 7,366 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  How to keep looping until the user input is valid ? KyawMyo 12 8,111 Jun-10-2019, 02:51 AM
Last Post: KyawMyo
  Dict overwrite previous list vincentspm 1 2,758 Mar-20-2019, 12:31 PM
Last Post: scidam
  [WinError 193] %1 is not a valid Win32 application edlipson 1 11,790 Jan-24-2018, 12:52 AM
Last Post: snippsat
  Calling list from previous function zykbee 1 3,447 Nov-13-2017, 12:10 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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