- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
Python â€“ Split List on next larger value
When it is required to split a list based on the next larger value, a list comprehension, the ‘iter’ method and the ‘islice’ methods are used.
Below is a demonstration of the same −
from itertools import islice my_list = [11, 22, 33, 34, 45, 26, 87,11] print("The list is :") print(my_list) length_to_split = [2, 5, 3] print("The split length list is :") print(length_to_split) temp = iter(my_list) my_result = [list(islice(temp, element)) for element in length_to_split] print("The result is :") print(my_result)
The list is : [11, 22, 33, 34, 45, 26, 87, 11] The split length list is : [2, 5, 3] The result is : [[11, 22], [33, 34, 45, 26, 87], ]
The required packages are imported into the environment.
A list of integers is defined and is displayed on the console.
Another list of integers is defined and is displayed on the console.
The ‘iter’ method is called on the list, and assigned to a variable.
A list comprehension is used to iterate over the elements, and the ‘islice’ method is used.
This is converted to a list and is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Remove list elements larger than a specific value using Python
- Custom list split in Python
- Python â€“ Split Strings on Prefix Occurrence
- Next Larger element in n-ary tree in C++
- Python â€“ Next N elements from K value
- Program to merge two sorted list to form larger sorted list in Python
- Python - Split list into all possible tuple pairs
- How to split string on whitespace in Python?
- Split List in C++
- How to split strings on multiple delimiters with Python?
- Program to arrange linked list nodes based on the value k in Python
- How do you split a list into evenly sized chunks in Python?
- How to Split given list and insert in excel file using Python?
- Python - Clearing list as dictionary value
- Python â€“ Index Value repetition in List