Python Forum
List index out of range error while accessing 2 lists in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List index out of range error while accessing 2 lists in python
#1
I have 2 Lists in my code.
data list is having len = 11
new_list is having len = 23

I have to search if a string from data list exists in new_list.. I can see that my target_data gets filled with data,but when i go to print it i get an error..

here is my code:

        print len(new_list)
		print len(data)
		
		for i in range(len(new_list)):
			parts = (new_list[i].split('|'))
			#print parts
			if (int(parts[8]) >= 1 or int(parts[9])  >= 1 or int(parts[11])  >= 1 ):
				variable = parts[1]
				variable = variable.strip()
				variable = variable + "']"
				#print variable
				for d in data:
					if variable in d:
						target_data.append(d)	
						print "ok"
					else:
						NODATA = 1
			else:
				NODATA = 1
		print target_data
Error:
23 11 ok ok ok ok ok ok ok ok ok ok ok list index out of range
How do i take care of this list out of range error?
Reply
#2
list indexes are 0-based, so the max index is len(some_list)-1. That said - don't use for i in range(len(new_list)) to iterate over list. Look at https://python-forum.io/Thread-Basic-Nev...n-sequence

In addition - you do some weird stuff like variable = variable + "']" Can you provide sample data for both lists and expected output?
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
#3
(Sep-29-2020, 05:08 AM)buran Wrote: list indexes are 0-based, so the max index is len(some_list)-1. That said - don't use for i in range(len(new_list)) to iterate over list. Look at https://python-forum.io/Thread-Basic-Nev...n-sequence

In addition - you do some weird stuff like variable = variable + "']" Can you provide sample data for both lists and expected output?

data list contents
Output:
RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 315 10 Port 'clka' is not registered [Hierarchy: 'block_ram_top.blk_mem_inst_0'] RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 315 10 Port 'clka' is not registered [Hierarchy: 'block_ram_top.blk_mem_inst_1'] RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 317 10 Port 'ena' is not registered [Hierarchy: 'block_ram_top.latch'] RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 317 10 Port 'ena' is not registered [Hierarchy: 'block_ram_top.ff1'] RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 319 10 Port 'wen' is not registered [Hierarchy: 'block_ram_top']
new_list contents
Output:
| block_ram_top | (top) | 23 | 23 | 0 | 0 | 138 | 0 | 2 | 0 | 4 | | (block_ram_top) | (top) | 7 | 7 | 0 | 0 | 9 | 0 | 0 | 0 | 0 | | blk_mem_inst_0 | blk_mem_gen_0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | | (blk_mem_inst_0) | blk_mem_gen_0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
variables that i extract in my code
Output:
inst_blk_mem_gen'] gnbram.gnativebmg.native_blk_mem_gen'] valid.cstr'] ramloop[0].ram.r'] prim_noinit.ram'] blk_mem_inst_1'] U0'] block_ram_top'] blk_mem_inst_0'] complex_multiplier_inst']
Expected target_data list: This should have all the data list items having the variable that i extract from the new_list items
Output:
RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 315 10 Port 'clka' is not registered [Hierarchy: 'block_ram_top.blk_mem_inst_0'] RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 315 10 Port 'clka' is not registered [Hierarchy: 'block_ram_top.blk_mem_inst_1'] RegInputOutput-ML Warning /sitework/soc/users/law/yeppy2/yeppy2chewy/yeppy2chewy_4ports/yeppy2/Spy_test_prj/mem/project_1/project_1.srcs/sources_1/ip/blk_mem_gen_0/synth/blk_mem_gen_0.vhd 319 10 Port 'wen' is not registered [Hierarchy: 'block_ram_top']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 739 May-15-2023, 03:28 PM
Last Post: buran
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,967 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Creating list of lists, with objects from lists sgrinderud 7 1,563 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  IndexError: list index out of range dolac 4 1,844 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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