# Append Method in Python

**URL:** <https://discuss.python.org/t/append-method-in-python/33176>\
**Category:** Python Help\
**Created:** [September 7, 2023, 2:39am UTC](https://discuss.python.org/t/append-method-in-python/33176 "2023-09-07T02:39:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 7, 2023, 2:39am UTC](https://discuss.python.org/t/append-method-in-python/33176/1 "2023-09-07T02:39:50Z")

</div>

I know this is a simple code, but somehow I kind of wonder why it is so…

my\_list = []

for i in range (5):  
my\_list.append (i + 1)

print(my\_list)

[1, 2, 3, 4, 5]

I was thinking of [2,3,4,5,6]. Because it is i+1 for each number from 1 to 5…  
Thank you in advance for the explanation.

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [September 7, 2023, 2:53am UTC](https://discuss.python.org/t/append-method-in-python/33176/2 "2023-09-07T02:53:44Z")

</div>

The `range()` function yields values which start from `0`, not `1`, by  
default. This matches the use of indices, which also count from `0`.

Try this:

```
 print(list(range(5)))

```

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![jhanarato](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jhanarato/32/14552_2.png) [@jhanarato](https://discuss.python.org/u/jhanarato)\
**Post date:** [September 7, 2023, 2:56am UTC](https://discuss.python.org/t/append-method-in-python/33176/3 "2023-09-07T02:56:13Z")

</div>

`range(5)` produces a sequence of 5 integers starting with `0`, not `1`

By providing the parameter `5` you’re saying: give me 5 numbers, starting from `0` and stopping when you get to `5`.

To append the numbers 1 to 5 you’d do this:

```py
my_list = []

for i in range(1, 6):
    my_list.append(i)

print(my_list)

```

This way the range gives you all the numbers from start to stop, but does not include stop. This is called a “half open range” in computing.

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 7, 2023, 3:02am UTC](https://discuss.python.org/t/append-method-in-python/33176/4 "2023-09-07T03:02:50Z")

</div>

Oh, starts from 0. I may have confused it again. Thank you for the reply!

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 7, 2023, 3:04am UTC](https://discuss.python.org/t/append-method-in-python/33176/6 "2023-09-07T03:04:04Z")

</div>

Thank you for the reply. I get it now.

---

<div class="post-metadata">

**Author:** ![jhanarato](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jhanarato/32/14552_2.png) [@jhanarato](https://discuss.python.org/u/jhanarato)\
**Post date:** [September 7, 2023, 3:38am UTC](https://discuss.python.org/t/append-method-in-python/33176/7 "2023-09-07T03:38:33Z")

</div>

These are also equivalent:

```py
# Convert an iterable to a list
my_list = list(range(1, 6))

# Use a list comprehension
my_list = [i for i in range(1, 6)]

```
