If seq
is a sequence, then:
def zigzag(seq):
return seq[::2], seq[1::2]
If seq
is a totally generic iterable, such as possibly a generator:
def zigzag(seq):
results = [], []
for i, e in enumerate(seq):
results[i%2].append(e)
return results
More Related Contents:
- Removing duplicates in lists
- Permutations between two lists of unequal length
- How to efficiently compare two unordered lists (not sets) in Python?
- Pythonic way to check if a list is sorted or not
- Detecting consecutive integers in a list [duplicate]
- How to efficiently compare two unordered lists (not sets)?
- In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique *while preserving order*? [duplicate]
- Checking if all elements in a list are unique
- Complexity of list.index(x) in Python
- Python: find closest string (from a list) to another string
- Sliding window maximum in O(n) time
- How can I match up permutations of a long list with a shorter list (according to the length of the shorter list)? [duplicate]
- Python – Removing overlapping lists
- Find non-common elements in lists
- convert string to list [duplicate]
- Pythonic way to print list items
- Non-alphanumeric list order from os.listdir()
- Sum of list of lists; returns sum list
- List of tuples to dictionary [duplicate]
- Group by multiple keys and summarize/average values of a list of dictionaries
- Python: Removing spaces from list objects [duplicate]
- How to remove multiple indexes from a list at the same time? [duplicate]
- How to multiply all integers inside list [duplicate]
- Group list by values [duplicate]
- Python: filtering lists by indices
- What are dict_keys, dict_items and dict_values?
- Ordered intersection of two lists in Python
- What to consider before subclassing list?
- What is the cost/ complexity of insert in list at some location?
- Check list monotonicity