My code -
s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
-
-
for k in range(len(s)):
-
st=s.count(s[k])
-
if st>=2:
-
print s.pop(s.index(s[k]))
-
-
Traceback (most recent call last):
-
File "C:/Python30/test.py", line 4, in <module>
-
st=s.count(s[k])
-
IndexError: list index out of range
-
how can I fix the error
IndexError: list index out of range
7 10751
My code -
s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
-
-
for k in range(len(s)):
-
st=s.count(s[k])
-
if st>=2:
-
print s.pop(s.index(s[k]))
-
-
Traceback (most recent call last):
-
File "C:/Python30/test.py", line 4, in <module>
-
st=s.count(s[k])
-
IndexError: list index out of range
-
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: -
>>> s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ss x','ssx','cc','cc']
-
>>> for item in s[:]:
-
... n = s.count(item)
-
... if n >= 2:
-
... print item
-
... s.remove(item)
-
...
-
aaaa
-
cc
-
>>> s
-
['cccccccc', 'dddd', 'ssss', 'xxxxx', 'aaaa', 'ss x', 'ssx', 'cc']
-
>>>
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: -
>>> s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ss x','ssx','cc','cc']
-
>>> for item in s[:]:
-
... n = s.count(item)
-
... if n >= 2:
-
... print item
-
... s.remove(item)
-
...
-
aaaa
-
cc
-
>>> s
-
['cccccccc', 'dddd', 'ssss', 'xxxxx', 'aaaa', 'ss x', 'ssx', 'cc']
-
>>>
Just as a note:
is exactly the same as
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.
My code -
s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
-
-
for k in range(len(s)):
-
st=s.count(s[k])
-
if st>=2:
-
print s.pop(s.index(s[k]))
-
-
Traceback (most recent call last):
-
File "C:/Python30/test.py", line 4, in <module>
-
st=s.count(s[k])
-
IndexError: list index out of range
-
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. - >>> s=['aaaa','cccccccc','dddd','ssss','xxxxx','aaaa','ssx','ssx','cc','cc']
-
>>> d=dict([(s[i], i) for i in range(len(s))])
-
>>> s=d.keys()
-
>>> print s
-
['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.
Just as a note:
is exactly the same as
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.
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!
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!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ishwor |
last post by:
i am trying to remove an item 'e' from the list l but i keep getting IndexError.
I know the size of the list l is changing in the for loop & its sort
of trivial task but i found no other way than...
|
by: Derek Schuff |
last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but
i couldnt find it.
I have some code like this:
for line in f:
toks = line.split()
try:
if int(toks,16) ==...
|
by: comp.lang.tcl |
last post by:
I have a TCL proc that needs to convert what might be a list into a
string to read
consider this:
]; # OUTPUTS Hello World which
is fine for PHP
]; # OUTPUT {{-Hello}}
World, which PHP...
|
by: cindy |
last post by:
I have a dynamic datagrid. I have custom classes for the controls
public class CreateEditItemTemplateDDL : ITemplate {
DataTable dtBind;
string strddlName;
string strSelectedID;
string...
|
by: erikwickstrom |
last post by:
Hi all,
I'm sorry about the newbie question, but I've been searching all
afternoon and can't find the answer!
I'm trying to get this bit of code to work without triggering the
IndexError.
...
|
by: BurnTard |
last post by:
Okay, so I know what an IndexError is and all, but that doesn't help me understand why on earth I'm getting this one. I'm writing a program that's supposed skim through directories, dump all files...
|
by: John Salerno |
last post by:
Is it possible to write a list comprehension for this so as to produce a
list of two-item tuples?
base_scores = range(8, 19)
score_costs =
print zip(base_scores, score_costs)
I can't think...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |