Slicing the list

The slicing of a list is the same as we did in tuples. See the syntax:

<list-name>[start : stop : step]

See the example:

>>> Avengers[1:3]

['iron-man', 'Captain']

>>> Avengers[:4]

['hulk', 'iron-man', 'Captain', 'Thor']

>>> Avengers[:]

['hulk', 'iron-man', 'Captain', 'Thor']

>>> Avengers[2:]

['Captain', 'Thor']

>>> list1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]

>>> list1[1:13:3]

[2, 5, 8, 11]

>>>

The step means the amount by which the index increases. If you don't define it, then it takes 1 step by default.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.67.5