5

Print first N items of a list/generator

This works for plain lists. It prints the first 3 items of the list

l=[1, 2, 3, 4]
print(l[:min(3, len(l))])

I want this work for lists with less then three items, too. If there is only one item in the list, then print one item.

AFAIK len(l) only works for lists. How to implement this for generators?

guettli
  • 25,042
  • 81
  • 346
  • 663
  • 6
    This will probably help with the generator question: [How to take the first N items from a generator or list in Python?](http://stackoverflow.com/questions/5234090/how-to-take-the-first-n-items-from-a-generator-or-list-in-python) – Keiwan Dec 22 '16 at 13:39
  • 1
    *"I want this work for lists with less then three items, too"* - ...it does? – jonrsharpe Dec 22 '16 at 13:46

3 Answers3

12

You can just use l[:3], no need for anything else:

In [11]: l = [1, 2, 3 ,4]

In [12]: print(l[:3])
[1, 2, 3]

This will work exactly the same for generators:

In [19]: def firstn(n):
    ...:     num = 0
    ...:     while num < n:
    ...:         yield num
    ...:         num += 1
    ...:

In [20]: list(firstn(5))[:3]
Out[20]: [0, 1, 2]

It also works for containers which have fewer than n elements:

n = 20
lyst = [1, 2]
print(lyst[:20])  # prints [1, 2]
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Question is for generator and not for List. – Ajeet Ganga Dec 22 '16 at 13:39
  • 1
    @Ajeet, just added the example for generators – Dekel Dec 22 '16 at 13:39
  • 1
    Generators are potentially infinitely long. It's also hugely inefficient to exhaust them *then* slice. – jonrsharpe Dec 22 '16 at 13:46
  • 2
    No need to reinvent the wheel. Just use `islice` from `itertools`. eg. `max(islice(iter(range(m)), n))` works for all possible positive integer value pairs of `m` and `n`. – Dunes Dec 22 '16 at 13:46
  • @Dunes, I'm not sure I understand what you are saying. Is it regarding the code of the generator? If so - this is only an example of the `[:3]` part on generators. – Dekel Dec 22 '16 at 13:48
  • What I'm saying is that `islice` provides the functionality provided by `firstn` and more. It mimics slicing functionality for generators. – Dunes Dec 22 '16 at 13:51
  • Got it. If the user only want to get the first N items I don't think the using the `itertools.islice` is required :) thats all – Dekel Dec 22 '16 at 13:53
  • 1
    But it is, for exactly the reasons I've already mentioned. – jonrsharpe Dec 22 '16 at 13:54
2

For lists you can use

l = [1, 2, 3, 4, 5]
print(l[:3])

This won't fail also for shorter lists. If you want to use generators, I think the best way is converting the generator to a list and doing the same

a = (i for i in range(0, 4))  # example of generator
print(list(a)[:3])

Also won't fail for shorter generators

If you want to get a generator after the slice, use itertools

import itertools
print(itertools.islice(generated_list, 3)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
-1

If I understand the question correctly, you want a generator to produce min(N,len(generator)).

Apply slice to generator directly. No need instantiate the list. This will produce min(N,len(generator)) lengthed generator.

e.g.

>>> l = range(5)
>>> l[:10]
range(0, 5)
>>>
Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79