0

How to select multiple elements from a list?

I think it is that code, but it's wrong. p = [1,2,3,4,5] print((p:3) returns[1,2,3])

8th Aug 2019, 12:03 PM
Vladimir
5 Answers
+ 3
I suggest to progress with the python lessons on sololearn and you get there quickly. Lists always begin with index 0. To get the first 3 elements (that have index 0,1 and 2) you use: p[:3] Check the lesson for more examples https://www.sololearn.com/learn/Python/2453/
8th Aug 2019, 2:01 PM
Tibor Santa
Tibor Santa - avatar
+ 5
What you need to study for it: slice!
8th Aug 2019, 12:29 PM
HonFu
HonFu - avatar
+ 2
You either need to use list slicing or to just index twice.
8th Aug 2019, 3:30 PM
Seb TheS
Seb TheS - avatar
+ 1
what do you mean by select? is that python? do you want a sub-list containing certain elements from a list?
8th Aug 2019, 12:07 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Yes, Anton, I wanna sub-list with some elements (I've thought output is 1,2,3, but now I think it is 2,3,4 [because position 1 have variable 2]) . I read it on the site: https://www.listendata.com/2017/05/JUMP_LINK__&&__python__&&__JUMP_LINK-data-science.html?m=1#spyder_shortcut
8th Aug 2019, 12:21 PM
Vladimir