# Replacing excessive conditionals with a list......maybe?

**URL:** <https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315>\
**Category:** Python Help\
**Created:** [October 1, 2026, 11:06pm UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315 "2026-10-01T23:06:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianofdoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/adrianofdoom/32/22114_2.png) [@adrianofdoom](https://discuss.python.org/u/adrianofdoom)\
**Post date:** [October 1, 2026, 11:06pm UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/1 "2026-10-01T23:06:28Z")

</div>

self.chk\_var verifies the type and length of a var and calls a method if correct, or prints an error message if not.  
self.\_chk\_var also returns a True if the type and length of the var is correct, False if it is not correct.  
Currently, I am ignoring the return values.

Code Snippet,

```python
        nbin=NBin()
        if not self.splice_event_cancel_indicator:
            self._chk_var(bool, nbin.add_flag, "out_of_network_indicator", 1)
            self._chk_var(bool, nbin.add_flag, "program_splice_flag", 1)
            self._chk_var(bool, nbin.add_flag, "duration_flag", 1)
            self._chk_var(bool, nbin.add_flag, "splice_immediate_flag", 1)
            self._chk_var(bool, nbin.add_flag, "event_id_compliance_flag", 1)
            nbin.forward(3)
            if self.program_splice_flag:
                if not self.splice_immediate_flag:
                    self._encode_splice_time(nbin)
            if self.duration_flag:                    
                self._encode_break_duration(nbin)
            self._chk_var(int, nbin.add_int, "unique_program_id", 16)
            self._chk_var(int, nbin.add_int, "avail_num", 8)
            self._chk_var(int, nbin.add_int, "avails_expected", 8)
        self.command_length = len(nbin.bites)
            return nbin.bites

```

Here’s the problem, I have about one hundred vars to verify, and if any of them are wrong I do Not want to execute the last two lines,

```python

        self.command_length = len(nbin.bites)
            return nbin.bites

```

There’s already a bunch of conditionals and with so many vars I do Not want to do a bunch of nested if statements like this

```python

    if self._chk_var(int, nbin.add_int, "unique_program_id", 16):
        if self._chk_var(int, nbin.add_int, "avail_num", 8):
            if self._chk_var(int, nbin.add_int, "avails_expected", 8):

```

My best idea so far is something like,

```python

        if not self.splice_event_cancel_indicator:

             # Check if False is in the list of return values

            if False in [
            self._chk_var(bool, nbin.add_flag, "out_of_network_indicator", 1),
            self._chk_var(bool, nbin.add_flag, "program_splice_flag", 1),
            self._chk_var(bool, nbin.add_flag, "duration_flag", 1),
            self._chk_var(bool, nbin.add_flag, "splice_immediate_flag", 1),
            self._chk_var(bool, nbin.add_flag, "event_id_compliance_flag", 1),
            ]:
                return False
            nbin.forward(3)
            if self.program_splice_flag:
                if not self.splice_immediate_flag:
                    self._encode_splice_time(nbin)
            if self.duration_flag:                    
                self._encode_break_duration(nbin)
            
            # Check if False is in the list
            
            if False in [    
            self._chk_var(int, nbin.add_int, "unique_program_id", 16),
            self._chk_var(int, nbin.add_int, "avail_num", 8),
            self._chk_var(int, nbin.add_int, "avails_expected", 8),
            ]:
                return False
        self.command_length = len(nbin.bites)
            return nbin.bites

```

## **Does anyone have a better idea?**

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [October 2, 2026, 12:19am UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/2 "2026-10-02T00:19:56Z")

</div>

How about this:

```python
if not self.splice_event_cancel_indicator:
    checks =
        (bool, nbin.add_flag, "out_of_network_indicator", 1),
        (bool, nbin.add_flag, "program_splice_flag", 1),
        (bool, nbin.add_flag, "duration_flag", 1),
        (bool, nbin.add_flag, "splice_immediate_flag", 1),
        (bool, nbin.add_flag, "event_id_compliance_flag", 1),
    ]

    if not all(self._chk_var(*args) for args in checks):
        return False

    nbin.forward(3)
    if self.program_splice_flag:
        if not self.splice_immediate_flag:
            self._encode_splice_time(nbin)
    if self.duration_flag:                    
        self._encode_break_duration(nbin)

    checks = [
        (int, nbin.add_int, "unique_program_id", 16),
        (int, nbin.add_int, "avail_num", 8),
        (int, nbin.add_int, "avails_expected", 8),
    ]

    if not all(self._chk_var(*args) for args in checks):
        return False
self.command_length = len(nbin.bites)
    return nbin.bites

```

---

<div class="post-metadata">

**Author:** ![adrianofdoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/adrianofdoom/32/22114_2.png) [@adrianofdoom](https://discuss.python.org/u/adrianofdoom)\
**Post date:** [October 2, 2026, 12:34am UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/3 "2026-10-02T00:34:02Z")

</div>

I like that, it does come out a cleaner and more readable.

If I put

```python
   if not all(self._chk_var(*args) for args in checks):
        return False

```

in a method like

````python
def check_these(self,checks):
    if not all(self._chk_var(*args) for args in checks):
        return False```

````

I can do

```python
    checks = [
        (int, nbin.add_int, "unique_program_id", 16),
        (int, nbin.add_int, "avail_num", 8),
        (int, nbin.add_int, "avails_expected", 8),
    ]
  if not self.check_these(checks):
      return False

```

**Thank you man, I like it.**

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [October 2, 2026, 12:56am UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/4 "2026-10-02T00:56:01Z")

</div>

A further change would be to have, say:

```python
    checks =
        (bool, "out_of_network_indicator", 1),
        (bool, "program_splice_flag", 1),
        (bool, "duration_flag", 1),
        (bool, "splice_immediate_flag", 1),
        (bool, "event_id_compliance_flag", 1),
    ]

```

and use a dict to tell you which of the `nbin.add_*` methods should be called:

```python
add_method = {bool: nbin.add_flag, int: nbin.add_int}

```

assuming that all of the `bool` and `int` checks will be using the appropriate method.

Just a thought!

---

<div class="post-metadata">

**Author:** ![peterc](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@peterc](https://discuss.python.org/u/peterc)\
**Post date:** [October 2, 2026, 10:03am UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/5 "2026-10-02T10:03:24Z")

</div>

three approaches to refactor this come to my mind:

1: use early returns  
example:

```python
def f(...) -> Error | int:
  if not self._chk_var(bool, nbin.add_flag, "out_of_network_indicator", 1):
    return Error("out of network indicator not bool")
  # more checks
  self.command_length = len(nbin.bites)
    return nbin.bites

```

2: use lists “more appropriately”  
example:

```python
bool_vars = ["out_of_network_indicator", "program_splice_flag", ...]
if any(self._chk_var(bool, nbin.add_flag, var_name, 1) for var_name in bool_vars): ...

```

3: raise errors

All of these techniques will avoid nesting a million if statements.

---

<div class="post-metadata">

**Author:** ![adrianofdoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/adrianofdoom/32/22114_2.png) [@adrianofdoom](https://discuss.python.org/u/adrianofdoom)\
**Post date:** [October 2, 2026, 10:56pm UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/6 "2026-10-02T22:56:41Z")

</div>

2 is what I wanted to do and it would work for boolean values , because they are all 1 bit, but I also have int, hex, and byte values of various lengths.

I’m going to think about 1 and 3 though.

Thank you for taking the time, I appreciate it.

---

<div class="post-metadata">

**Author:** ![adrianofdoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/adrianofdoom/32/22114_2.png) [@adrianofdoom](https://discuss.python.org/u/adrianofdoom)\
**Post date:** [October 2, 2026, 11:02pm UTC](https://discuss.python.org/t/replacing-excessive-conditionals-with-a-list-maybe/109315/7 "2026-10-02T23:02:31Z")

</div>

I didn’t even think about that, you think since they have names like “add\_int”  
I would have thought of that, but I didn’t.

I like that, because I really hate how long the calls to \_chk\_var are, a lot of the variable names are so long but they are from a spec so I thought it best to keep them as the come.
