[input[i:i+n] for i in range(0, len(input), n)] # Use xrange in py2k
where n
is the length of a chunk.
Since you don’t define what might happen to the final element of the new list when the number of elements in input
is not divisible by n
, I assumed that it’s of no importance: with this you’ll get last element equal 2 if n
equal 7, for example.
Related Contents:
- Concatenating two lists – difference between ‘+=’ and extend()
- Pythonic way to return list of every nth item in a larger list
- Test if lists share any items in python
- How to check if all of the following items are in a list?
- Extract list of attributes from list of objects in python
- Making a list of evenly spaced numbers in a certain range in python
- How do I get a list of indices of non zero elements in a list?
- How to insert the contents of one list into another
- What’s the most Pythonic way to identify consecutive duplicates in a list?
- How do I concatenate two lists in Python?
- List of lists changes reflected across sublists unexpectedly
- How to define a two-dimensional array?
- Is there a short contains function for lists?
- Remove all the elements that occur in one list from another
- How to unzip a list of tuples into individual lists? [duplicate]
- Permutations between two lists of unequal length
- data.frame rows to a list
- Matrix Transpose in Python [duplicate]
- I want to exception handle ‘list index out of range.’
- How to convert list of key-value tuples into dictionary?
- python filter list of dictionaries based on key value
- Head and tail in one line
- First Python list index greater than x?
- Get number of items from list (or other iterable) with certain condition
- Python: How to remove empty lists from a list? [duplicate]
- Converting a String to a List of Words?
- Why does Python start at index -1 (as opposed to 0) when indexing a list from the end? [duplicate]
- How to convert list of numpy arrays into single numpy array?
- How can I use list comprehensions to process a nested list?
- Python -Intersection of multiple lists?
- Merge multiple column values into one column in python pandas
- django – convert a list back to a queryset [duplicate]
- Python: filtering lists by indices
- Remove list element without mutation
- ‘list’ object has no attribute ‘shape’
- How to get every first element in 2 dimensional list
- Adding an item to an immutable Seq
- Changing values of a list of namedtuples
- Python self-referential list produces weird output on sorted
- Efficient method to calculate the rank vector of a list in Python
- Remove object from a list of objects in python
- How can I convert string values from a dictionary, into int/float datatypes?
- join two lists of dictionaries on a single key
- Unpack list to variables
- docker restart container failed: “already in use”, but there’s no more docker image
- Python: Unpacking an inner nested tuple/list while still getting its index number
- Change a string of integers separated by spaces to a list of int [duplicate]
- How does one enable selections in SwiftUI’s List
- call list of function using list comprehension
- Replace special characters in a string in Python