First, your question. Like @ Vasiliy said, you have 3 indices. The n is alright, since you protect it with your while condition. The 1 is fine since enumerate always generates 2. You can either handle the error with a try/ except block: with open( filename, ' r', newline= ' ' ) as f: reader = csv. reader( f) # read file row by row for row in reader: try: print( row[ 1] ) except IndexError, e: # Do something here to exError: list index out of range - Python 3. " r" ) data = file. split( " \ n" ) for i in data: val = i. append( [ val[ 0] ] ) y. append( [ val[ 1] ] ) print( " Data = " + str( data) ) print( " X Axis = " + str( x) ) print( " Y Axis = " + str( y) ). Generally it means that you are providing an index for which a list element does not exist. g, if your list was [ 1, 3, 5, 7], and you asked for the element at index 10, you would be well out of bounds and receive an error, as only elements 0.
39; list index out of range' most likely means you are referring to n- th element of the list, while the length of the list is. check with items that comes between/ middle in range, and insure that their index is not last if you get output. if len( hoge) > = 3: print( hoge[ 2] ). 長さが2以下の場合には hoge[ 3: ] が空タプルになる ことを利用 if hoge[ 3: ] : print( hoge[ 3] ). try文を利用 try: print( hoge[ 2] ) except: None. 強制的にリストの3番目( になるかもしれない) 要素を作る ( この場合3. IndexError: list index out of range. Pythonのリストと比較する場合、 Javaの配列より java. ArrayListクラスの方. # coding: utf- 8 a = [ 2, 6, 8] # リストの 読み込み print( a[ 0] ) # リストの更新 a.
append( 10) print( a) # リスト. The error is that your list indices are out of range- - that is, you are trying to refer to some index that doesn' t even exist. Without debugging your. I' m assuming you' re using python 3 based on the your use of print( ) and input( ) :. You' re getting an index out of bounds exception because you are deleting an item from an array you' re iterating over. After you delete that item, len( token) is 4, but your for loop is iterating 5 times ( 5 being returned from the. You can avoid your problem with this way: > > > for k, v in zip( l[ : : 2], l[ 1: : 2] ) :. print( k, ' : ', v). ( ' a', ' : ', 52) ( ' b', ' : ', 30) ( ' c', ' : ', 36). Anyway, here is more elegant and just right way to do that you want: > > > d = { ' a' : 52, ' b' : 30, ' c' : 36}.