[docs] list.insert(i,x) behavior

Georg Brandl georg at python.org
Wed Jan 14 08:48:19 CET 2015


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 01/12/2015 10:28 AM, pankaj garg wrote:
> Hi Sir/Madam
> 
> I am trying following stuff with lists.
> 
> /*Line 1*/ >>> a = [1,2,3,4,5] */Line 2/* >>> a.insert(5,6) */Line 3/* >>>
> a */Line 5/*        [1,2,3,4,5,6] */Line 6/* >>> a.insert(9,9) */Line 7/*
> >>> a */Line 8/*        [1,2,3,4,5,6,9]
> 
> Output shouldn't be [1,2,3,4,5,6,0,0,0,9] as index specified is */index =
> 9/* */ /* I checked this on */Python 3.4.2/*

Hi Pankaj,

the insert method will only ever insert one item into the list.
Therefore indices greater than len(a) will be capped to it.

This is similar to how slices work: too-large indices are
capped to the highest possible index.

cheers,
Georg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlS2H0MACgkQN9GcIYhpnLDFFgCZAXuKxx/p26/I+1QcQsce4q7B
dkAAmgIJwjpYpTv2akafW/D4WtM9vjZE
=h2GF
-----END PGP SIGNATURE-----


More information about the docs mailing list