# PEP Proposal: list.first() and list.last() for accessing most important list elements

**URL:** <https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532>\
**Category:** Ideas\
**Created:** [September 20, 2024, 3:48pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532 "2024-09-20T15:48:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![unexcellent](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/unexcellent/32/22647_2.png) [@unexcellent](https://discuss.python.org/u/unexcellent)\
**Post date:** [September 20, 2024, 3:48pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/1 "2024-09-20T15:48:45Z")

</div>

# Proposal

Create methods `mylist.first()` and `mylist.last()` for `list` as aliases for `mylist[0]` and `mylist[-1]` respectively.

# Example

```python
mylist = [0, 1, 2, 3]
assert mylist.first() == 0
assert mylist.last() == 3

empty_list = []
empty_list.first() # raises "IndexError: list index out of range" or "IndexError: list is empty"

```

# Why?

The first and last elements of a list are accessed the most by index directly. By aliasing them through clearly denoted functions readability is improved especially for new Python programmers.

Some other programming languages that already implement this:

- Rust with [Vector::first()](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.first)
- C++ with [list::front()](https://cplusplus.com/reference/list/list/front/)

What do you think? Should I create a PEP for that?

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 20, 2024, 3:53pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/2 "2024-09-20T15:53:09Z")

</div>

This seems totally unnecessary? Using the indexes is shorter and will be faster (since these methods are going to do the same thing but with the overhead of a method call)

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [September 20, 2024, 3:55pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/3 "2024-09-20T15:55:46Z")

</div>

> [@unexcellent](#):
>
> The first and last elements of a list are accessed the most by index directly. By aliasing them through clearly denoted functions readability is improved especially for new Python programmers.

New programmers are better advised to learn Python’s features directly. Indexing is powerful and flexible, and a feature like this would simply delay new users getting access to that power.

> [@unexcellent](#):
>
> This would also add consistency with other programming languages

Consistency with other languages is not a useful goal in itself. You need to demonstrate that these methods would be useful in Python itself, not just claim “consistency” is sifficient. For example, a `first` method on lists would not work on tuples or deques. In Rust, `first` is generic, so it’s useful on all collection types.

> [@unexcellent](#):
>
> What do you think? Should I create a PEP for that?

No, I think you’d be better dropping the idea, to be honest.

---

<div class="post-metadata">

**Author:** ![unexcellent](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/unexcellent/32/22647_2.png) [@unexcellent](https://discuss.python.org/u/unexcellent)\
**Post date:** [September 20, 2024, 4:03pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/4 "2024-09-20T16:03:12Z")

</div>

Readability is one of Python’s basic principles. I would argue that the shortness of `mylist[0]` can hinder readability compared to `mylist.first()`.

And yes, these methods would be functionally redundant just like `mylist[0]` is functionally redundant when you can use `mylist. __getitem__ (0)`. It is not about functional necessity but ergonomics.

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 20, 2024, 4:08pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/5 "2024-09-20T16:08:26Z")

</div>

As Paul pointed out, `mylist[0]` should be _very_ readable for anyone who is comfortable in Python, because indexing is so common (as _you_ pointed out!). Giving newcomers an idiosyncratic method for a single use-case, rather than moving them towards fluency, doesn’t seem like an improvement.

In any case, you certainly shouldn’t write a PEP without a core developer who is willing to sponsor it (which I suspect is…very unlikely to happen).

---

<div class="post-metadata">

**Author:** ![unexcellent](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/unexcellent/32/22647_2.png) [@unexcellent](https://discuss.python.org/u/unexcellent)\
**Post date:** [September 20, 2024, 4:10pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/6 "2024-09-20T16:10:07Z")

</div>

> [@pf\_moore](#):
>
> New programmers are better advised to learn Python’s features directly. Indexing is powerful and flexible, and a feature like this would simply delay new users getting access to that power.

Not for reading however. Seeing code like `mylist.last()` should immediately convey the intention (especially with a docstring). `mylist[-1]` on the other hand takes some experience to interpret.

> [@pf\_moore](#):
>
> Consistency with other languages is not a useful goal in itself. You need to demonstrate that these methods would be useful in Python itself, not just claim “consistency” is sifficient. For example, a `first` method on lists would not work on tuples or deques. In Rust, `first` is generic, so it’s useful on all collection types.

That is true. I made that point to show that `first()`and `last()` would not be radically new concepts in programming languages.

---

<div class="post-metadata">

**Author:** ![unexcellent](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/unexcellent/32/22647_2.png) [@unexcellent](https://discuss.python.org/u/unexcellent)\
**Post date:** [September 20, 2024, 4:12pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/7 "2024-09-20T16:12:08Z")

</div>

> [@jamestwebber](#):
>
> In any case, you certainly shouldn’t write a PEP without a core developer who is willing to sponsor it (which I suspect is…very unlikely to happen).

That is why I am asking here first 🙂

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [September 20, 2024, 6:22pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/8 "2024-09-20T18:22:26Z")

</div>

Your proposed version of `list.first()` would work identically to `[0]` indexing: an `IndexError` would be raised if the list was empty. This would actually be _inconsistent_ with Rust’s implementation of these methods for `Vec`. In Rust, indexing into an empty vec panics (analogous to `IndexError` in Python), but `Vec<T>::first` returns `Option<T>`, for which the closest analogy in Python would be `T | None`.

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [September 20, 2024, 6:37pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/9 "2024-09-20T18:37:14Z")

</div>

Somewhat related:

> [@Add safe \`.get\` method to List](https://discuss.python.org/t/add-safe-get-method-to-list/32555):
>
> To access an item in a dictionary, you use indexing: d["some\_key"]. However, if the key doesn’t exist in the dictionary, a KeyError is raised. To avoid this, you can use .get and pass in a default value to return instead: d.get("some\_key", "default value"). Lists don’t have such a method for safely indexing. There are many ways around it (as documented in many SO threads: [thread 1](https://stackoverflow.com/questions/5125619/why-doesnt-list-have-safe-get-method-like-dictionary), [thread 2](https://stackoverflow.com/questions/2492087/how-to-get-the-nth-element-of-a-python-list-or-a-default-if-not-available)), but they’re all pretty awkward and they aren’t composable or chain-able the same way that .get() is. T…

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [September 20, 2024, 8:17pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/10 "2024-09-20T20:17:15Z")

</div>

`first` and `last` are the kind of methods an abstract data type _implemented_ by lists might provide, but there’s no particular reason the list itself needs to expose them.

---

<div class="post-metadata">

**Author:** ![sayandipdutta](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sayandipdutta/32/21584_2.png) [@sayandipdutta](https://discuss.python.org/u/sayandipdutta)\
**Post date:** [September 20, 2024, 9:05pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/11 "2024-09-20T21:05:56Z")

</div>

I would rather like `Iterable.first` and `Iterable.last`.

- Although `iteraTOR.first` is just another way to spell `next(iterator)`, `iteraBLE.first` would forgo the need to do `next(iter(iterable), default)` or `for-break` trick.
- Whereas, `Iterator.last` would be quite handy to consume an iterator in C speed, without needing to import `collections.deque`.
- Ideally both `first` and `last` would throw exception if not provided with a default in case of empty iterables.

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [September 20, 2024, 9:59pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/12 "2024-09-20T21:59:49Z")

</div>

> [@sayandipdutta](#):
>
> I would rather like `Iterable.first` and `Iterable.last`.

`Sequence.last` would probably make more sense than `Iterator.last`.

---

<div class="post-metadata">

**Author:** ![sayandipdutta](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sayandipdutta/32/21584_2.png) [@sayandipdutta](https://discuss.python.org/u/sayandipdutta)\
**Post date:** [September 21, 2024, 7:56am UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/13 "2024-09-21T07:56:39Z")

</div>

Do you say that because `Iterator.last` would fail to stop in case of infinite iterators? Or because it has a potential of people not understanding the fact that it will always be O(n)? Or the fact that it will consume the entire iterator?

If it’s any of the above, I think it is the same for `list(iterator)` or `deque(iterator, maxlen=1|0)`. But I agree, the only benefit of `iterator.last` is avoiding `deque` import, which is not a huge win. Also, I now see, how `iterator.last` could be a footgun, whereas at least the deque trick is a better indicator of the fact that user knows what they are doing. And in case of `IteraBLE.first/last`, the deal-breaker is that `Set` and `Mapping` are are also iterable.

Another case I just thought of, that could be deemed problematic is: `iterable.first/last` may break expectation; it would modify Iterator, but not Sequence. So if there are more operations to follow, type narrowing becomes necessary.

For `Sequence`, personally, I can’t remember having to deal with a sequence where I couldn’t get the first and the last by `[0|-1]`. At least whenever I write a Sequence, I implement support for `[-1]`. But I have felt the need of `first` and `last` for iterables on multiple occasions (mainly for elegance). I think it all comes down to Iterators not having functionalities like `peek` and `advance` (which have been brought up and shot down multiple times). But I realize I am going horribly off-topic, and throwing a lot of ideas all at once, so I will stop.

---

<div class="post-metadata">

**Author:** ![Paddy3118](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/paddy3118/32/1396_2.png) [@Paddy3118](https://discuss.python.org/u/Paddy3118)\
**Post date:** [September 21, 2024, 8:22am UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/14 "2024-09-21T08:22:23Z")

</div>

> [@unexcellent](#):
>
> What do you think?

Hi, and whilst I _would_ like to extend my welcome to our community… …I **don’t** think this is a good idea. Python’s indexing capabilities are powerful, succinct, and not too hard to learn I think. Python language community try not to have too many , (sanctioned), ways of doing one thing to avoid confusion. Whilst mention of first and last might be necessary in absolute beginners code, you could augment it with a comment before it becomes unneccessary.  
If your audience is none-Python programmers then write pseudo-code which should expand single-language idioms for clarity, and should not expect to be directly runnable in and of themselves.  
If your audience are fellow Python programmers then expecting them to know accessing first and last elements of a list by index is OK. If you know your “Python” audience would have problems reading this then you should add extra comments describing this - but use the indexing to help them learn; and probably need more care in other aspects of Python shown too.

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [September 21, 2024, 9:22am UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/15 "2024-09-21T09:22:09Z")

</div>

I just want to remind people that it’s technology impossible to add methods to either of iterables or iterators, since these are duck typed protocols defined by having ` __iter__ `/` __next__ `\[1\], with no further requirements.

While it might be fun to ponder what the semantics of these methods could be, the best we could hope for without a fundamental change to python semantics is a builtin function, or more likely, a function in itertools. (Or a method on a few builtin iterators/-ables, but I don’t think anyone considers that a benefit)

* * *

1. or a compatible ` __getitem__ `

---

<div class="post-metadata">

**Author:** ![unexcellent](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/unexcellent/32/22647_2.png) [@unexcellent](https://discuss.python.org/u/unexcellent)\
**Post date:** [September 21, 2024, 10:51am UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/16 "2024-09-21T10:51:48Z")

</div>

Yes, but to my understanding raising an `IndexError` is more pythonic than returning `None`. This is where the philosophies of Python and Rust clash. Rust expects you to handle the `Option` type while it would be easy to miss this silent failure in Python.

Or would you prefer returning `None` instead of raising an exception?

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [September 21, 2024, 5:01pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/18 "2024-09-21T17:01:48Z")

</div>

I agree that it’s more common in Python for failure to be indicated by raising exceptions rather than returning `None`. But if we say it’s going to work exactly the same way as indexing in Python, that removes one of the key reasons why Rust has both indexing and a `Vec::first` method (so that the language offers a type-safe method of accessing the first element of a maybe-empty vec, as well as the possibly-panicking method that is indexing)

---

<div class="post-metadata">

**Author:** ![avi.gross](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@avi.gross](https://discuss.python.org/u/avi.gross)\
**Post date:** [September 21, 2024, 5:46pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/19 "2024-09-21T17:46:06Z")

</div>

A method adds overhead and if it contributes no additional functionality, and someone thinks using `0` or `-1` as indices is too difficult, then why not do something trivial like set up some _constants_ of sorts like:

```python
FIRST = 0
LAST = -1
SECOND = 1
THIRD = 2
PENULTIMATE = -2

# Now use these instead of mylist.first(), ...
mylist[FIRST]
mylist[LAST]
mylist[SECOND]
mylist[PENULTIMATE]

```

I will say anyone wanting to learn a language like python really should learn the indexing techniques as asking for something like all but the first does not lend itself to this approach.

Sometimes making things simpler can merely make it more complicated.

---

<div class="post-metadata">

**Author:** ![avi.gross](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@avi.gross](https://discuss.python.org/u/avi.gross)\
**Post date:** [September 21, 2024, 9:25pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/20 "2024-09-21T21:25:40Z")

</div>

Slightly shifting from what the OP raised, I am asking for examples of what value may be added by having or adding sort of getters for the first or last elements of the list while leaving the list unchanged?

I mean, for example, if I had my own data structure and I wanted to intercept access to the data and do _something_ such as check if request should be honored, or log that it happened or slow them down by only allowing one request per minute, or record how many times it has been asked for, or even note if the underlying content has changed since the last time and return a cached copy if not, I can imagine such interventions making sense.

But a python list is simply a list. It is designed to do very little and do it fairly well. If you want other functionality, you may be able to subclass it or use some other data construct.

What the OP, who may be un-excellent, has not mentioned may actually be valid. If the focus is on new users, and especially those whose background (or intial programming language) has a prejudice that numbering start with the number 1, then he may have a point.

`mylist[1]` is how many languages would access the first. Some do not really have any way to access the last except by some stratagem like `mylist[len(mylist)]` and a possible crutch when starting to use python lists might help them avoid having to learn the bitter reality of a zero based left-to right indexing with a negative number based right to left indexing that we all know and love!

---

<div class="post-metadata">

**Author:** ![funkyfuture](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/funkyfuture/32/19385_2.png) [@funkyfuture](https://discuss.python.org/u/funkyfuture)\
**Post date:** [September 21, 2024, 10:15pm UTC](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532/21 "2024-09-21T22:15:24Z")

</div>

i have a use-case where the `.first` and `.last` properties of an empty sequence are fine to be `None` values. but it would be wrong beahviour for sequences generally. context matters with such higher level (regarding the expressivenes compared to index notation) attributes and it’s thus recommedable to implement case-related extensions based on abstract base classes.

[Next page](https://discuss.python.org/t/pep-proposal-list-first-and-list-last-for-accessing-most-important-list-elements/64532.md?page=2)
