Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1
Hi Team,

I am facing below error while executing the code . Dont know where exactly the error exists . Could you please help me on this

#Reading Data from Files into Collections
orders_file = open('/content/sample_data/orders.txt')
orders_raw = orders_file.read()
# here orders_raw is read as String file
orders = orders_raw.splitlines()
# It will convert each row into string (orders) and put it in list
#len(orders)
#orders[0]
orders[0:10]
#for i in orders[0:10]: print(i)

def get_customer_orders(orders,customer_id):
    orders_filtered = []
    for i in orders:
        if int(i.split(',')[2])==customer_id:
            orders_filtered.append(i)
    return orders_filtered
Error Details

---------------------------------------------------------------------------
Error:
IndexError Traceback (most recent call last) <ipython-input-40-d6ac476436e2> in <module>() ----> 1 print(get_customer_orders(orders,11599)) <ipython-input-39-eacfbc89ef92> in get_customer_orders(orders, customer_id) 2 orders_filtered = [] 3 for i in orders: ----> 4 if int(i.split(',')[2])==customer_id: 5 print(i.split(',')[2]) 6 orders_filtered.append(i) IndexError: list index out of range
Data
----

Output:
['1,2013-07-25 00:00:00.0,11599,CLOSED', '2,2013-07-25 00:00:00.0,256,PENDING_PAYMENT', '3,2013-07-25 00:00:00.0,12111,COMPLETE', '4,2013-07-25 00:00:00.0,8827,CLOSED', '5,2013-07-25 00:00:00.0,11318,COMPLETE', '6,2013-07-25 00:00:00.0,7130,COMPLETE', '7,2013-07-25 00:00:00.0,4530,COMPLETE', '8,2013-07-25 00:00:00.0,2911,PROCESSING', '9,2013-07-25 00:00:00.0,5657,PENDING_PAYMENT', '10,2013-07-25 00:00:00.0,5648,PENDING_PAYMENT']
buran write Jan-24-2021, 01:46 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
In my system, this code works as expected. Can you post all of the code that is creating the error?

orders_file = open('orders.txt')
orders_raw = orders_file.read()
orders_file.close ()
orders = orders_raw.splitlines()
 
def get_customer_orders(orders,customer_id):
	orders_filtered = []
	for i in orders:
		if int(i.split(',')[2])==customer_id:
			orders_filtered.append(i)
	return orders_filtered

print (get_customer_orders (orders, 256))
Output:
["'2,2013-07-25 00:00:00.0,256,PENDING_PAYMENT',"]
Reply
#3
check your data. is it possible that you have blank/incomplete lines?
your code works for sample data
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range Laplace12 1 241 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 1,140 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 404 Mar-25-2021, 07:20 PM
Last Post: buran
  Changing Index of 2 List in python giddyhead 0 399 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  string index out of range jade_kim 4 552 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  IndexError: index 10 is out of bounds for axis 0 with size 1 vahid89 1 2,158 Jan-07-2021, 06:19 PM
Last Post: deanhystad
Question Matching variable to a list index Gilush 17 1,175 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  How to get index of minimum element between 3 & 8 in list Mekala 2 770 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 723 Nov-04-2020, 11:26 AM
Last Post: Aggam
  List index out of range error while accessing 2 lists in python K11 2 649 Sep-29-2020, 05:24 AM
Last Post: K11

Forum Jump:

User Panel Messages

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