Python Forum
IndexError: tuple index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: tuple index out of range
#1
tp=(1,2,3,4,5,6,7,8,9,10)
for i in tp:
    lt = []
    if tp[i]%2 == 0:
        lt.append(tp[i])
    else:
	    pass
tp2 = tuple(lt)
print(tp2)
I don't understand why is i out of range.

After change in code I receive the same message
tp=(1,2,3,4,5,6,7,8,9,10)
for i in tp:
    lt = []
    for i in range(1,11):
        if tp[i]%2 == 0:
            lt.append(tp[i])
        else:
	        pass
tp2 = tuple(lt)
print(tp2)
Reply
#2
What is the complete error message which shows which line the error is on? Print i right after the for i in tp, and then print tp[i], which will error, so print on a separate line. You don't want 2 statements here as the for i in tp is already accessing the values in tp. There are additional problems in the next for i in tp, but that is for you to figure out.
Reply
#3
Because you are using the values from the tuple as an index to the elements in the same tuple.
tp contains 10 elements. But tp[10] is the 11th element ( indexing starts from 0 ) and of course, it doesn't exist.

Change the if statement.
if i % 2 == 0:
    lt.append(i)
In the first code snippet.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
wavic, the output is now (10,)

(Mar-26-2018, 12:50 AM)woooee Wrote: What is the complete error message which shows which line the error is on? Print i right after the for i in tp, and then print tp[i], which will error, so print on a separate line. You don't want 2 statements here as the for i in tp is already accessing the values in tp. There are additional problems in the next for i in tp, but that is for you to figure out.

tp=(1,2,3,4,5,6,7,8,9,10)
for i in tp:
    print(i)
    print(tp[i])
    lt = []
    if i%2 == 0:
        lt.append(i)
    else:
        pass
tp2 = tuple(lt)
print(tp2)
it gives line 4
Reply
#5
Again, do not use the values as an index!

How the output is (10,)? What have you tried exactly?

Here is the same code. You can skip the else statement.
>>> lt = []
>>> tp = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> for i in tp:
...     if i % 2 == 0:
...         lt.append(i)
...     else:
...         pass
... 
>>> tp2 = tuple(lt)
>>> print(tp2)
(2, 4, 6, 8, 10)
I see now that your lt = [] is inside the for loop. So on every iteration you wipe it out to an empty list
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
lt = []
tp=(1,2,3,4,5,6,7,8,9,10)
for i in tp:
    
    if i%2 == 0:
        lt.append(i)
   
tp2 = tuple(lt)
print(tp2)
Now it works and I completely understand it. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,117 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,347 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,397 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,923 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,357 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,448 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,224 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,855 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,321 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,232 Jun-22-2021, 10:47 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020