# Indexable get method. \[1,2,3\].get(4) # None

**URL:** <https://discuss.python.org/t/indexable-get-method-1-2-3-get-4-none/53430>\
**Category:** Ideas\
**Created:** [May 15, 2024, 5:42pm UTC](https://discuss.python.org/t/indexable-get-method-1-2-3-get-4-none/53430 "2024-05-15T17:42:18Z")\
**Posts on this page:** 1\
**Showing post:** 116

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [June 8, 2024, 8:49am UTC](https://discuss.python.org/t/indexable-get-method-1-2-3-get-4-none/53430/116 "2024-06-08T08:49:40Z")

</div>

One more case I have encountered today, where this would have been a good fit.

```python
l = [0, 0, 0, 0, 1]
trimmed_zeros = list(itertools.dropwhile(operator.not_, l))

```

Now I need first value in the list with a fallback. Then, if it is lower than 10, I need to take the 2nd with the fallback to 1st if it doesn’t exist. With `list.get`:

```python
optimal_unit = trimmed_zeros.get(0, FALLBACK)
if optimal_unit < 10:
    optimal_unit = trimmed_zeros.get(1, optimal_unit)

```

Without it (superfluous length checks):

```python
:
optimal_unit = trimmed_zeros[0] if len(trimmed_zeros) else FALLBACK
if optimal_unit < 10 and len(trimmed_zeros) > 1:
    optimal_unit = trimmed_zeros[1]

```

Or this (creating iterator for 2 values just doesn’t feel right):

```python
it = iter(trimmed_zeros)
optimal_unit = next(it, FALLBACK)
if optimal_unit < 10:
    optimal_unit = next(it, optimal_unit)

```

---

_[View the full topic](https://discuss.python.org/t/indexable-get-method-1-2-3-get-4-none/53430)._
