# Need help with List code

**URL:** <https://forum.codewithmosh.com/t/need-help-with-list-code/15096>\
**Category:** Python\
**Created:** [September 12, 2022, 11:06am UTC](https://forum.codewithmosh.com/t/need-help-with-list-code/15096 "2022-09-12T11:06:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MariaK](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MariaK](https://forum.codewithmosh.com/u/MariaK)\
**Post date:** [September 12, 2022, 11:06am UTC](https://forum.codewithmosh.com/t/need-help-with-list-code/15096/1 "2022-09-12T11:06:58Z")

</div>

Hi I am new to python and doing some online practice exercises on different websites.  
I came across this code with multiple choice questions.  
Now the following is a list with 4 numbers .This is the question.

**numbers = [2,3,5,8]**

now the user/student has to select two options that will not raise exceptions.

The first answer is :  
**numbers[numbers[1]**

Explanation given on the website is as follows:

**is actually numbers[3] which evaluates to 8 and no exception is raised.**  
Now I don’t understand how it evaluates to 8 .I thought the answer would be 3 as index[1] has number 3.Can someone please explain ?

Also I want to know why this option will raise exception  
**numbers[numbers[3]]**

Explanation on the website is as follows:

is actually numbers[8] which will raise exception.Why?

Please help me its very annoying I can’t move before I understand this code. 🙂

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 12, 2022, 1:33pm UTC](https://forum.codewithmosh.com/t/need-help-with-list-code/15096/2 "2022-09-12T13:33:59Z")

</div>

For starters we need to understand what the code fundamentally does. For any `numbers[numbers[x]]` we are getting the value at one index (`x`) and then using that value to get the value at another index. If we broke it up into two lines (and added some variable names) it would look like this:

```python
y = numbers[x]
result = numbers[y]

```

So `y` is the value stored at the original index (`x`) and that number is used as a new index in the same list to get our `result`.

Now applying the original `numbers[numbers[1]]`:

```python
y = numbers[1] # y = 3
result = numbers[y] # result = numbers[3] = 8

```

This works because the value at `numbers[1]` happens to be a valid index in the `numbers` list. But look what happens with `numbers[numbers[3]]`:

```python
y = numbers[3] # y = 8
result = numbers[y] # result = numbers[8] = uh oh

```

In that case the value at `numbers[3]` was 8 which is not a valid index in the `numbers` list so we get an exception.

---

<div class="post-metadata">

**Author:** ![MariaK](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@MariaK](https://forum.codewithmosh.com/u/MariaK)\
**Post date:** [September 20, 2022, 11:40am UTC](https://forum.codewithmosh.com/t/need-help-with-list-code/15096/3 "2022-09-20T11:40:22Z")

</div>

Thank you so much.You explained it so well.Thanks a lot 🙂
