
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Custom list split in Python
Data analytics throws complex scenarios where the data need to be wrangled to moved around. In this context let’s see how we can take a big list and split it into many sublists as per the requirement. In this article we will explore the approaches to achieve this.
With zip and for loop
In this approach we use list dicing to get the elements from the point at which the splitting has to happen. Then we use zip and for loop to create the sublists using a for loop.
Example
Alist = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] # The indexes to split at split_points = [2, 5, 8] # Given list print("Given list : " + str(Alist)) # SPlit at print("The points of splitting : ",split_points) #Perform the split split_list = [Alist[i: j] for i, j in zip([0] + split_points, split_points + [None])] # printing result print("The split lists are : ", split_list)
Output
Running the above code gives us the following result −
Given list : ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] The points of splitting : [2, 5, 8] The split lists are : [['Mon', 'Tue'], ['Wed', 6, 7], ['Thu', 'Fri', 11], [21, 4]]
Using chain and zip
The chain function makes an iterator that returns elements from the first iterable until it is exhausted. So it marks the points where the splitting happens. Then we use the zip function to package the result of splitting into sublists.
Example
from itertools import chain Alist = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] # The indexes to split at split_points = [2, 5, 8] # Given list print("Given list : ", str(Alist)) # Split at print("The points of splitting : ",split_points) # to perform custom list split sublists = zip(chain([0], split_points), chain(split_points, [None])) split_list = list(Alist[i : j] for i, j in sublists) # printing result print("The split lists are : ", split_list)
Output
Running the above code gives us the following result −
Given list : ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] The points of splitting : [2, 5, 8] The split lists are : [['Mon', 'Tue'], ['Wed', 6, 7], ['Thu', 'Fri', 11], [21, 4]]
- Related Articles
- Python Program to Split a String by Custom Lengths
- Python - Convert List to custom overlapping nested list
- Custom Multiplication in list of lists in Python
- Custom sorting in list of tuples in Python
- Python – Custom Lower bound a List
- Python - Custom space size padding in Strings List
- Python – Split List on next larger value
- Split List in C++
- Python - Split list into all possible tuple pairs
- Python Program to Split a List into Two Halves
- Python program to remove row with custom list element
- How do you split a list into evenly sized chunks in Python?
- How to Split given list and insert in excel file using Python?
- Custom len() Function In Python
- Custom length Matrix in Python
