开发者

simple python list comprehension question

开发者 https://www.devze.com 2023-01-30 18:35 出处:网络
i am trying to select the elements of a list without the very first element. the following code works but it kinda look ugly to me

i am trying to select the elements of a list without the very first element. the following code works but it kinda look ugly to me

[s[i] for i in range(len(s)) if i>0]

is the开发者_开发技巧re a better way to write it? thanks


Use the slicing notation:

s[1:]

Alternatively, you can avoid copying the list thus:

itertools.islice(s, 1, None)

The result isn't a list — it doesn't support random access, for instance — but you can pass it to anything that accepts an iterator.


Wouldn't s[1:] be correct?

0

精彩评论

暂无评论...
验证码 换一张
取 消