Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i reference values from various ranges within a list?

Tags:

python

list

slice

What I want to do is reference several different ranges from within a list, i.e. I want the 4-6th elements, the 12 - 18th elements, etc. This was my initial attempt:

test = theList[4:7, 12:18]

Which I would expect to give do the same thing as:

test = theList[4,5,6,12,13,14,15,16,17]

But I got a syntax error. What is the best/easiest way to do this?

like image 207
lukehawk Avatar asked Dec 19 '22 02:12

lukehawk


1 Answers

You can add the two lists.

>>> theList = list(range(20))
>>> theList[4:7] + theList[12:18]
[4, 5, 6, 12, 13, 14, 15, 16, 17]
like image 69
Bhargav Rao Avatar answered Dec 26 '22 10:12

Bhargav Rao