Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Extract Elements in a Given Range from a List

Python Extract Elements in a Given Range from a List

Document Object Reference Manual Python3 Examples

Suppose we have a list my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we want to extract the elements whose indices range from 2 to 6 (including indices 2 and 6). We can do this with Python's slice operation.

Example

my_list

=

[

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

,

10

]

extracted_elements

=

my_list

[

2

:

7

]

print

(

extracted_elements

)

Code analysis:

Output result:

Example

[

3

,

4

,

5

,

6

,

7

]

Document Object Reference Manual Python3 Examples