By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,198 Members | 1,698 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,198 IT Pros & Developers. It's quick & easy.

IndexError: list index out of range

P: 90
My code
Expand|Select|Wrap|Line Numbers
  1. s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
  2.  
  3. for k in range(len(s)):
  4.     st=s.count(s[k])
  5.     if st>=2:
  6.        print s.pop(s.index(s[k]))
  7.  
  8. Traceback (most recent call last):
  9.   File "C:/Python30/test.py", line 4, in <module>
  10.     st=s.count(s[k])
  11. IndexError: list index out of range
  12.  
how can I fix the error
IndexError: list index out of range
Oct 5 '07 #1
Share this Question
Share on Google+
7 Replies


bartonc
Expert 5K+
P: 6,596
My code
Expand|Select|Wrap|Line Numbers
  1. s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
  2.  
  3. for k in range(len(s)):
  4.     st=s.count(s[k])
  5.     if st>=2:
  6.        print s.pop(s.index(s[k]))
  7.  
  8. Traceback (most recent call last):
  9.   File "C:/Python30/test.py", line 4, in <module>
  10.     st=s.count(s[k])
  11. IndexError: list index out of range
  12.  
how can I fix the error
IndexError: list index out of range
That would be because of the pop(). When you shorten the list that you are indexing on you cause the error.

By using a slice for beginning to end, you work on a copy. I threw in some other options which avoid the index all together:
Expand|Select|Wrap|Line Numbers
  1. >>> s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ss  x','ssx','cc','cc']
  2. >>> for item in s[:]:
  3. ...     n = s.count(item)
  4. ...     if n >= 2:
  5. ...         print item
  6. ...         s.remove(item)
  7. ...         
  8. aaaa
  9. cc
  10. >>> s
  11. ['cccccccc', 'dddd', 'ssss', 'xxxxx', 'aaaa', 'ss  x', 'ssx', 'cc']
  12. >>> 
Oct 5 '07 #2

Motoma
Expert 2.5K+
P: 3,237
That would be because of the pop(). When you shorten the list that you are indexing on you cause the error.

By using a slice for beginning to end, you work on a copy. I threw in some other options which avoid the index all together:
Expand|Select|Wrap|Line Numbers
  1. >>> s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ss  x','ssx','cc','cc']
  2. >>> for item in s[:]:
  3. ...     n = s.count(item)
  4. ...     if n >= 2:
  5. ...         print item
  6. ...         s.remove(item)
  7. ...         
  8. aaaa
  9. cc
  10. >>> s
  11. ['cccccccc', 'dddd', 'ssss', 'xxxxx', 'aaaa', 'ss  x', 'ssx', 'cc']
  12. >>> 
Just as a note:

Expand|Select|Wrap|Line Numbers
  1. for item in s[:]:
  2.  
is exactly the same as

Expand|Select|Wrap|Line Numbers
  1. for item in s:
  2.  
The only reason I mention this is that when learning Python, I often stumbled when I would encounter the whole array-subarray-step-bracket-thingies.
Oct 5 '07 #3

KaezarRex
P: 52
My code
Expand|Select|Wrap|Line Numbers
  1. s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
  2.  
  3. for k in range(len(s)):
  4.     st=s.count(s[k])
  5.     if st>=2:
  6.        print s.pop(s.index(s[k]))
  7.  
  8. Traceback (most recent call last):
  9.   File "C:/Python30/test.py", line 4, in <module>
  10.     st=s.count(s[k])
  11. IndexError: list index out of range
  12.  
how can I fix the error
IndexError: list index out of range
Here is a way to get rid of duplicate items in a list that probably isn't good form, but I thought it was interesting. I put everything in a dictionary, which can't have duplicate keys, and then retrieved the list of keys.
Expand|Select|Wrap|Line Numbers
  1. >>> s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
  2. >>> d=dict([(s[i], i) for i in range(len(s))])
  3. >>> s=d.keys()
  4. >>> print s
  5. ['cccccccc', 'ssss', 'ssx', 'cc', 'aaaa', 'dddd', 'xxxxx']
If you were using an ordered list this way wont work because the dictionary keys are in an unordered list.
Oct 5 '07 #4

KaezarRex
P: 52
Just as a note:

Expand|Select|Wrap|Line Numbers
  1. for item in s[:]:
  2.  
is exactly the same as

Expand|Select|Wrap|Line Numbers
  1. for item in s:
  2.  
The only reason I mention this is that when learning Python, I often stumbled when I would encounter the whole array-subarray-step-bracket-thingies.
It's actually different (and very useful) because "s[:]" creates a whole new list (a deep copy) to iterate through, instead of iterating over "s" itself. That way the loop can modify "s" without throwing an error.
Oct 5 '07 #5

Motoma
Expert 2.5K+
P: 3,237
It's actually different (and very useful) because "s[:]" creates a whole new list (a deep copy) to iterate through, instead of iterating over "s" itself. That way the loop can modify "s" without throwing an error.
Oh man...Owned.

You learn something new every day. Thanks for the lesson KaesarRex!
Oct 5 '07 #6

bartonc
Expert 5K+
P: 6,596
It's actually different (and very useful) because "s[:]" creates a whole new list (a deep copy) to iterate through, instead of iterating over "s" itself. That way the loop can modify "s" without throwing an error.
Actually, Motoma has a point. When working with arrays, the elements are passed by reference (even in a slice). Lists and arrays are not interchangeable that way!
Oct 5 '07 #7

KaezarRex
P: 52
Actually, Motoma has a point. When working with arrays, the elements are passed by reference (even in a slice). Lists and arrays are not interchangeable that way!
Looks like I've learned something too. Thanks for the clarification.
Oct 5 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.