Sửa lỗi value is out of range 0 maya 2023 năm 2024

Our global network of 1.4 million neighbors, friends, and leaders volunteer their skills and resources to solve issues and address community needs.

Take Action With Us

1.4 million

We connect people

1.4 million members worldwide

47 million

We transform communities

Approximately 47 million volunteer hours each year

$333 million

We fund sustainable projects

$333 million awarded for global service initiatives in 2020-21

News & Features

Help us create lasting change

Solving some of the world’s most complex and pressing problems takes real commitment and vision. Rotary members believe that we share a responsibility to take action to improve our communities. Join us, so that we can make an even bigger impact – together.

Get involved

No challenge is too big for us

For more than a century, we’ve bridged cultures and connected continents. We champion peace, fight illiteracy and poverty, help people get access to clean water and sanitation, and fight disease. Our newest cause is to protect our planet and its resources.

Your trusted source for Minnesota news today. Read articles, view photos, or watch videos about news in Minneapolis, St. Paul, Twin Cities Metro areas, St. Cloud, Rochester, and beyond. The Star Tribune is committed to provide more of what matters to Minnesotans. All day. Every day. Your trusted source for Minnesota news today. Read articles, view photos, or watch videos about news in Minneapolis, St. Paul, Twin Cities Metro areas, St. Cloud, Rochester, and beyond. The Star Tribune is committed to provide more of what matters to Minnesotans. All day. Every day.

This means that the first item in a list has an index of 0, the second item has an index of 1, and so on.

You can use the index number to access the individual item.

To access an item in a list using its index number, first write the name of the list. Then, inside square brackets, include the intiger that corresponds with the item's index number.

Taking the example from earlier, this is how you would access each item inside the list using its index number:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

You can also use negative indexing to access items inside lists in Python.

To access the last item, you use the index value of -1. To acces the second to last item, you use the index value of -2.

Here is how you would access each item inside a list using negative indexing:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

Why does the

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

0 error occur in Python?

Using an index number that is out of the range of the list

You'll get the

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

0 error when you try and access an item using a value that is out of the index range of the list and does not exist.

This is quite common when you try to access the last item of a list, or the first one if you're using negative indexing.

Let's go back to the list we've used so far.

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

Say I want to access the last item, "Lenny", and try to do so by using the following code:

print[names[4]]

# output

# Traceback [most recent call last]:
#  File "/Users/dionysialemonaki/python_articles/demo.py", line 3, in 
#    print[names[4]]

# IndexError: list index out of range

Generally, the index range of a list is

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

2, with

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

3 being the total number of values in the list.

With the total values of the list above being

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

4, the index range is

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

5.

Now, let's try to access an item using negative indexing.

Say I want to access the first item in the list, "Kelly", by using negative indexing.

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
print[names[-5]]

# output

# Traceback [most recent call last]:
#  File "/Users/dionysialemonaki/python_articles/demo.py", line 3, in 
#    print[names[-5]]

# IndexError: list index out of range

When using negative indexing, the index range of a list is

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

6, where

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

7 the total number of items contained in the list.

With the total number of items in the list being

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

4, the index range is

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

9.

Using the wrong value in the

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

0 function in a Python

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

1 loop

You'll get the

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

0 error when iterating through a list and trying to access an item that doesn't exist.

One common instance where this can occur is when you use the wrong integer in Python's

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

0 function.

The

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

0 function typically takes in one integer number, which indicates where the counting will stop.

For example,

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

5 indicates that the counting will start from

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

6 and end at

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[0] # Kelly
names[1] # Nelly
names[2] # Jimmy
names[3] # Lenny

4.

So, by default, the counting starts at position

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

6, is incremented by

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

9 each time, and the number is up to – but not including – the position where the counting will stop.

Let's take the following example:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
for name in range[5]:
    print[names[name]]

# output

# Kelly

# Nelly

# Jimmy

# Lenny

# Traceback [most recent call last]:
#  File "/Users/dionysialemonaki/python_articles/demo.py", line 7, in 
#   print[names[name]]

# IndexError: list index out of range

Here, the list

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

# create a variable called name_length to store the length of the list
name_length = len[names]

# print value of variable to the console
print[name_length]

# output

# 4

6 has four values.

I wanted to loop through the list and print out each value.

When I used

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

5 I was telling the Python interpreter to print the values that are at the positions

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

2.

However, there is no item in position 4.

You can see this by first printing out the number of the position and then the value at that position.


# 0

# Kelly

# 1

# Nelly

# 2

# Jimmy

# 3

# Lenny

# 4

# Traceback [most recent call last]:
#  File "/Users/dionysialemonaki/python_articles/demo.py", line 8, in 
#    print[names[name]]

# IndexError: list index out of range

You see that at position

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

6 is "Kelly", at position

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

9 is "Nelly", at position

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

5 is "Jimmy" and at position

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

6 is "Lenny".

When it comes to position four, which was specified with

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

5 which indicates positions of

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

2, there is nothing to print out and therefore the interpreter throws an error.

One way to fix this is to lower the integer in

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

0:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
for name in range[4]:
    print[name]
    print[names[name]]

# output

# 0

# Kelly

# 1

# Nelly

# 2

# Jimmy

# 3

# Lenny

Another way to fix this when using a

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

1 loop is to pass the length of the list as an argument to the

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

0 function. You do this by using the

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

# create a variable called name_length to store the length of the list
name_length = len[names]

# print value of variable to the console
print[name_length]

# output

# 4

8 built-in Python function, as shown in an earlier section:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

# create a variable called name_length to store the length of the list
name_length = len[names]

# print value of variable to the console
print[name_length]

# output

# 4

0

When passing

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

# create a variable called name_length to store the length of the list
name_length = len[names]

# print value of variable to the console
print[name_length]

# output

# 4

8 as an argument to

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]
names[-4] # Kelly
names[-3]# Nelly
names[-2] # Jimmy
names[-1] # Lenny

0, make sure that you don't make the following mistake:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

# create a variable called name_length to store the length of the list
name_length = len[names]

# print value of variable to the console
print[name_length]

# output

# 4

1

After running the code, you'll again get an

print[names[4]]

# output

# Traceback [most recent call last]:
#  File "/Users/dionysialemonaki/python_articles/demo.py", line 3, in 
#    print[names[4]]

# IndexError: list index out of range

5 error:

names = ["Kelly", "Nelly", "Jimmy", "Lenny"]

# create a variable called name_length to store the length of the list
name_length = len[names]

# print value of variable to the console
print[name_length]

# output

# 4

2

Conclusion

Hopefully this article gave you some insight into why the

print[names[4]]

# output

# Traceback [most recent call last]:
#  File "/Users/dionysialemonaki/python_articles/demo.py", line 3, in 
#    print[names[4]]

# IndexError: list index out of range

5 error occurs and some ways you can avoid it.

If you want to learn more about Python, check out freeCodeCamp's Python Certification. You'll start learning in an interacitve and beginner-friendly way. You'll also build five projects at the end to put into practice and help reinforce what you learned.

Thanks for reading and happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Chủ Đề