List insert at index that is well out of range - behaves like append
I had a list a = [1, 2, 3] when I did a.insert(100, 100) [1, 2, 3, 100] as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed . Should it not throw IndexError: list assignment index out of range exception as it throws when I attempt doing a[100] = 100 Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ? Personal Opinion : Lets see how other dynamic languages behave in such a situation : Ruby : > a = [1, 2] > a[100] = 100 > a => [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100] The way ruby handles this is pretty clear and sounds meaningful (and this is how I expected to behave and it behaved as per my expectation) at least to me . So what I felt was either it should throw exception or do the way ruby handles it . Is ruby way of handling not the obvious way ? I even raised it in stackoverflow http://stackoverflow.com/questions/25840177/list-insert-at-index-that-is-wel... and got some responses .
On Mon, Sep 15, 2014 at 6:18 AM, Harish Tech
I had a list
a = [1, 2, 3]
when I did
a.insert(100, 100)
[1, 2, 3, 100]
as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .
Should it not throw
IndexError: list assignment index out of range
exception as it throws when I attempt doing
a[100] = 100
Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?
Personal Opinion : Lets see how other dynamic languages behave in such a situation : Ruby :
> a = [1, 2]
> a[100] = 100
> a
=> [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100]
The way ruby handles this is pretty clear and sounds meaningful (and this is how I expected to behave and it behaved as per my expectation) at least to me . So what I felt was either it should throw exception or do the way ruby handles it .
Is ruby way of handling not the obvious way ?
I even raised it in stackoverflow http://stackoverflow.com/questions/25840177/list-insert-at-index-that-is-wel...
and got some responses .
Hello Harish, The appropriate place to ask questions like this is python-list [1], or perhaps Stack Overflow. If you meant to suggest changing the behavior of Python in such cases, you should first discuss this on python-list, and then post a clearly written suggestion to python-ideas [2]. This list, python-dev, is used for discussing the development *of* the Python language. See the "Python Mailing Lists" page [3] for more information. Regards, - Tal Einat ..[1]: http://mail.python.org/mailman/listinfo/python-list ..[2]: http://mail.python.org/mailman/listinfo/python-ideas ..[3]: https://www.python.org/community/lists/
Hi ,
Sorry for that mistake . Now I have posted it in python-list mailing
list .
Thanks for your guidance.
Harish
On Mon, Sep 15, 2014 at 5:01 PM, Tal Einat
I had a list
a = [1, 2, 3]
when I did
a.insert(100, 100)
[1, 2, 3, 100]
as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was
to insert in an index that did not even existed .
Should it not throw
IndexError: list assignment index out of range
exception as it throws when I attempt doing
a[100] = 100
Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?
Personal Opinion : Lets see how other dynamic languages behave in such a situation : Ruby :
> a = [1, 2]
> a[100] = 100
> a
=> [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100]
The way ruby handles this is pretty clear and sounds meaningful (and
On Mon, Sep 15, 2014 at 6:18 AM, Harish Tech
wrote: trying this is how I expected to behave and it behaved as per my expectation) at least to me . So what I felt was either it should throw exception or do the way ruby handles it .
Is ruby way of handling not the obvious way ?
I even raised it in stackoverflow
http://stackoverflow.com/questions/25840177/list-insert-at-index-that-is-wel...
and got some responses .
Hello Harish,
The appropriate place to ask questions like this is python-list [1], or perhaps Stack Overflow.
If you meant to suggest changing the behavior of Python in such cases, you should first discuss this on python-list, and then post a clearly written suggestion to python-ideas [2].
This list, python-dev, is used for discussing the development *of* the Python language.
See the "Python Mailing Lists" page [3] for more information.
Regards, - Tal Einat
..[1]: http://mail.python.org/mailman/listinfo/python-list ..[2]: http://mail.python.org/mailman/listinfo/python-ideas ..[3]: https://www.python.org/community/lists/
On 15/09/14 12:31, Tal Einat wrote:
On Mon, Sep 15, 2014 at 6:18 AM, Harish Tech
wrote: I had a list
a = [1, 2, 3]
when I did
a.insert(100, 100)
[1, 2, 3, 100]
as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .
Should it not throw
IndexError: list assignment index out of range
exception as it throws when I attempt doing
a[100] = 100
Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?
Personal Opinion : Lets see how other dynamic languages behave in such a situation : Ruby :
> a = [1, 2]
> a[100] = 100
> a
=> [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100]
The way ruby handles this is pretty clear and sounds meaningful (and this is how I expected to behave and it behaved as per my expectation) at least to me . So what I felt was either it should throw exception or do the way ruby handles it .
Is ruby way of handling not the obvious way ?
I even raised it in stackoverflow http://stackoverflow.com/questions/25840177/list-insert-at-index-that-is-wel...
and got some responses .
Hello Harish,
The appropriate place to ask questions like this is python-list [1], or perhaps Stack Overflow.
I think this is an OK forum for this question. If someone isn't sure if something is a bug or not, then why not ask here before reporting it on the bug tracker? This does seem strange behaviour, and the documentation for list.insert gives no clue as to why this behaviour was chosen. Cheers, Mark.
On 15/09/2014 23:29, Mark Shannon wrote:
On 15/09/14 12:31, Tal Einat wrote:
On Mon, Sep 15, 2014 at 6:18 AM, Harish Tech
wrote: I had a list
a = [1, 2, 3]
when I did
a.insert(100, 100)
[1, 2, 3, 100]
as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .
Should it not throw
IndexError: list assignment index out of range
exception as it throws when I attempt doing
a[100] = 100
Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?
Personal Opinion : Lets see how other dynamic languages behave in such a situation : Ruby :
> a = [1, 2]
> a[100] = 100
> a
=> [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100]
The way ruby handles this is pretty clear and sounds meaningful (and this is how I expected to behave and it behaved as per my expectation) at least to me . So what I felt was either it should throw exception or do the way ruby handles it .
Is ruby way of handling not the obvious way ?
I even raised it in stackoverflow http://stackoverflow.com/questions/25840177/list-insert-at-index-that-is-wel...
and got some responses .
Hello Harish,
The appropriate place to ask questions like this is python-list [1], or perhaps Stack Overflow.
I think this is an OK forum for this question. If someone isn't sure if something is a bug or not, then why not ask here before reporting it on the bug tracker?
This does seem strange behaviour, and the documentation for list.insert gives no clue as to why this behaviour was chosen.
Cheers, Mark.
I assume it's based on the concepts of slicing. From the docs "s.insert(i, x) - inserts x into s at the index given by i (same as s[i:i] = [x])". Although shouldn't that read s[i:i+1] = [x] ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
On 09/15/2014 03:46 PM, Mark Lawrence wrote:
On 15/09/2014 23:29, Mark Shannon wrote:
I think this is an OK forum for this question.
It isn't. ;)
If someone isn't sure if something is a bug or not, then why not ask here before reporting it on the bug tracker?
The first stop should still be the main Python list, or Python Dev would be inundated with questions about why this or that doesn't work the same way as <insert other language here>. If the responses from Python list indicate that it is (or probably is) a bug, then possibly a post here to verify -- but a bug-tracker entry at that point is quite reasonable.
This does seem strange behaviour, and the documentation for list.insert gives no clue as to why this behaviour was chosen.
I assume it's based on the concepts of slicing. From the docs "s.insert(i, x) - inserts x into s at the index given by i (same as s[i:i] = [x])". Although shouldn't that read s[i:i+1] = [x] ?
No. If it was `s[i:i+1]` then the ith element would be replaced by the inserted object. -- ~Ethan~
On Mon, 15 Sep 2014 23:46:03 +0100
Mark Lawrence
I assume it's based on the concepts of slicing. From the docs "s.insert(i, x) - inserts x into s at the index given by i (same as s[i:i] = [x])". Although shouldn't that read s[i:i+1] = [x] ?
No, the latter would replace the contents at index i, while the former inserts it (formally, it replaces the 0-length slice with a 1-length slice). Regards Antoine.
On Mon, Sep 15, 2014 at 3:46 PM, Mark Lawrence
I assume it's based on the concepts of slicing. From the docs "s.insert(i, x) - inserts x into s at the index given by i (same as s[i:i] = [x])".
Ah, right. It matches thigs like s[100:] which is the empty string if s is shorter than 100.
Although shouldn't that read s[i:i+1] = [x] ?
Should've stopped while you were ahead. :-) 'Nuff said. -- --Guido van Rossum (python.org/~guido)
This functionality has existed since the earliest days of Python, and even if we all agreed it was wrong we couldn't change it -- it would just break too much existing code. I can't quite remember why I did it that way but it was definitely a conscious choice; probably some symmetry or edge case. (Note that it works this way at the other end too -- a.insert(-100, x) will insert x at the beginning of a, if a has fewer than 100 elements.) On Mon, Sep 15, 2014 at 3:29 PM, Mark Shannon wrote:
On 15/09/14 12:31, Tal Einat wrote:
On Mon, Sep 15, 2014 at 6:18 AM, Harish Tech
wrote: I had a list
a = [1, 2, 3]
when I did
a.insert(100, 100)
[1, 2, 3, 100]
as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .
Should it not throw
IndexError: list assignment index out of range
exception as it throws when I attempt doing
a[100] = 100
Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?
Personal Opinion : Lets see how other dynamic languages behave in such a situation : Ruby :
> a = [1, 2]
> a[100] = 100
> a
=> [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100]
The way ruby handles this is pretty clear and sounds meaningful (and this is how I expected to behave and it behaved as per my expectation) at least to me . So what I felt was either it should throw exception or do the way ruby handles it .
Is ruby way of handling not the obvious way ?
I even raised it in stackoverflow http://stackoverflow.com/questions/25840177/list- insert-at-index-that-is-well-out-of-range-behaves-like-append
and got some responses .
Hello Harish,
The appropriate place to ask questions like this is python-list [1], or perhaps Stack Overflow.
I think this is an OK forum for this question. If someone isn't sure if something is a bug or not, then why not ask here before reporting it on the bug tracker?
This does seem strange behaviour, and the documentation for list.insert gives no clue as to why this behaviour was chosen.
Cheers, Mark.
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ guido%40python.org
-- --Guido van Rossum (python.org/~guido)
participants (7)
-
Antoine Pitrou
-
Ethan Furman
-
Guido van Rossum
-
Harish Tech
-
Mark Lawrence
-
Mark Shannon
-
Tal Einat