# Meaning of square brackets

**URL:** <https://discuss.python.org/t/meaning-of-square-brackets/20294>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 24, 2022, 11:40pm UTC](https://discuss.python.org/t/meaning-of-square-brackets/20294 "2022-10-24T23:40:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dazza000](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dazza000](https://discuss.python.org/u/dazza000)\
**Post date:** [October 24, 2022, 11:40pm UTC](https://discuss.python.org/t/meaning-of-square-brackets/20294/1 "2022-10-24T23:40:26Z")

</div>

HI I wonder if any one can help me understanding the use of the square brackets on the end of this statement.  
mail\_ids = mails[0].decode().split()[-2:]  
How does the [-2:] work?  
thanks  
dazza000

---

<div class="post-metadata">

**Author:** ![dazza000](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dazza000](https://discuss.python.org/u/dazza000)\
**Post date:** [October 25, 2022, 12:15am UTC](https://discuss.python.org/t/meaning-of-square-brackets/20294/2 "2022-10-25T00:15:22Z")

</div>

Ok, I think I have got it now. it is about the indexing of a list. I think it is referring to the last two elements of the list called mails.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 25, 2022, 12:16am UTC](https://discuss.python.org/t/meaning-of-square-brackets/20294/3 "2022-10-25T00:16:16Z")

</div>

`mails[0].decode()` probably returns a string.

`mails[0].decode().split()` probably splits that string into a list of substrings.

`mails[0].decode().split()[-2:]` gives you the sub-list of the last two items from that list.

For example:

```auto
>>> L = "My hovercraft is full of eels.".split()
>>> L[-2:]
['of', 'eels.']

```

This is called “slicing”.

---

<div class="post-metadata">

**Author:** ![JDLH](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JDLH](https://discuss.python.org/u/JDLH)\
**Post date:** [October 27, 2022, 12:16am UTC](https://discuss.python.org/t/meaning-of-square-brackets/20294/4 "2022-10-27T00:16:01Z")

</div>

> [@dazza000](#):
>
> help me understanding the use of the square brackets

The first thing to understand is that the output from `split()` is a _list_. See the [documentation for `str.split()`](https://docs.python.org/3/library/stdtypes.html#str.split). A list is a _sequence type_.

The second thing to understand is the meaning of square brackets after a _sequence type_ is a _slice_. The meaning is defined in the Python **Library Reference** , section [**Common Sequence Operations**](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations). See especially note (4) after the table.

I encourage you to read and re-read the [**Python Documentation** , `https://docs.python.org/3/`](https://docs.python.org/3/). I have been programming in Python for many years, and I still refer back to the documentation all. the. time. It is really helpful.

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 27, 2022, 7:23am UTC](https://discuss.python.org/t/meaning-of-square-brackets/20294/5 "2022-10-27T07:23:05Z")

</div>

> [@JDLH](#):
>
> The first thing to understand is that the output from `split()` is a _list_. See the [documentation for `str.split()`](https://docs.python.org/3/library/stdtypes.html#str.split).

That is just guessing. `split()` is a method here. You do not know if it is being called on a `str` object in the code. That is why Steven used the word “probably”:

> [@steven.daprano](#):
>
> `mails[0].decode()` probably returns a string.
> 
> `mails[0].decode().split()` probably splits that string into a list of substrings.
