206

Is there a way we can fetch first 10 results from a list. Something like this maybe:

list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

list.fetch(10)

?

satoru
  • 29,529
  • 29
  • 85
  • 136
Amyth
  • 31,549
  • 25
  • 88
  • 134

4 Answers4

409
list[:10]

will give you the first 10 elements of this list using slicing.

However, note, it's best not to use list as a variable identifier as it's already used by Python: list()

To find out more about these type of operations you might find this tutorial on lists helpful and the link @DarenThomas provided Explain Python's slice notation - thanks Daren)

Community
  • 1
  • 1
Levon
  • 129,246
  • 33
  • 194
  • 186
  • 1
    this is the canonical answer. check here too: http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation – Daren Thomas Jun 05 '12 at 12:31
  • 5
    this one was surely a fast race for the first answer :) damn CAPTCHA! – Not_a_Golfer Jun 05 '12 at 12:32
  • Wow, thanks Daren , that was easy ! :-) – Amyth Jun 05 '12 at 12:32
  • actually what got me was that on top of the captcha, my original answer was too short. – Not_a_Golfer Jun 05 '12 at 12:33
  • Note that this creates a copy of the first 10 elements. Depending on your needs you might prefer lazy, non-copying slicing with itertools.islice. – georg Jun 05 '12 at 12:34
  • @DarenThomas I'll add your link to my answer, I hope you don't mind. – Levon Jun 05 '12 at 12:49
  • 1
    @thg435 -- This doesn't create a copy of the elements in the list, only a new reference to them. It does however, create a new list ... – mgilson Jun 05 '12 at 12:49
  • @mgilson: it depends on what you understand under "element". Python containers always hold pointers to objects, and this creates 10 new pointers (+ one new list object). – georg Jun 05 '12 at 12:52
  • 2
    @thg435 -- python has no pointers. (if you want pointers, you use C ;) python has references. the point here is that you don't create new objects, only new references to them...We're saying the same thing, but the way your original statement was written was misleading (at least to me) so I thought I would clarify. – mgilson Jun 05 '12 at 12:58
  • 1
    @mgilson: agreed, I should have better written "creates a copy of that part of the list". – georg Jun 05 '12 at 13:01
  • @Levon definitely ! I am sorry i tried it before, but so told me that i had to wait :p – Amyth Jun 05 '12 at 20:52
29

check this

 list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

 list[0:10]

Outputs:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
BookOfGreg
  • 3,374
  • 39
  • 53
user1409289
  • 412
  • 4
  • 13
18

The itertools module has lots of great stuff in it. So if a standard slice (as used by Levon) does not do what you want, then try the islice function:

from itertools import islice
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
iterator = islice(l, 10)
for item in iterator:
    print item
Spencer Rathbun
  • 13,966
  • 5
  • 51
  • 72
12

Use the slicing operator:

list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
list[:10]
ddk
  • 1,763
  • 1
  • 14
  • 18