How to iterate over the first n elements of a list?

后端 未结 4 563
悲哀的现实
悲哀的现实 2020-12-08 06:09

Say I\'ve got a list and I want to iterate over the first n of them. What\'s the best way to write this in Python?

4条回答
  •  离开以前
    2020-12-08 06:53

    I'd probably use itertools.islice (<- follow the link for the docs), which has the benefit of working with any iterable object.

提交回复
热议问题