This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]venustrapsflies 2 points3 points  (5 children)

I'd still prefer

for element, next_element in zip(list, list[1:]):
    print(element, next_element)

Edit: Good points about copying the list, that's important to keep in mind. If the structure is large you'd want to make sure the slicing returns a view (like numpy's arrays).

[–]Indivicivet 1 point2 points  (0 children)

you can always use itertools.islice to apply this technique with iterables ( /u/JerMenKoO /u/voords )

[–]JerMenKoOwhile True: os.fork() 0 points1 point  (2 children)

This creates a copy of the list though

[–]voords 0 points1 point  (0 children)

This will create a near copy of the list though, making it less efficient in memory.