- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Chunk Tuples to N in Python
When it is required to chunk the tuples to 'N' values, list comprehension is used.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
Below is a demonstration of the same −
my_tuple_1 = (87, 90, 31, 85,34, 56, 12, 5) print("The first tuple is :") print(my_tuple_1) N = 2 print("The value of 'N' has been initialized") my_result = [my_tuple_1[i : i + N] for i in range(0, len(my_tuple_1), N)] print("The tuple after chunking is : ") print(my_result)
The first tuple is : (87, 90, 31, 85, 34, 56, 12, 5) The value of 'N' has been initialized The tuple after chunking is : [(87, 90), (31, 85), (34, 56), (12, 5)]
- A tuple is defined, and is displayed on the console.
- The value of 'N' is initialized.
- The tuple is iterated over, using the 'range' method, and is divided into chunks using the '' brackets, i.e indexing.
- It is then converted to a list type.
- This result is assigned to a value.
- It is displayed as output on the console.
- N element incremental tuples in Python
- Repeating tuples N times in Python
- Trim tuples by N elements in Python
- Remove tuples from list of tuples if greater than n in Python
- How to Concatenate tuples to nested tuples in Python
- How to split Python tuples into sub-tuples?
- Combining tuples in list of tuples in Python
- Ways to concatenate tuples in Python
- Count tuples occurrence in list of tuples in Python
- Python program to find Tuples with positive elements in List of tuples
- Updating Tuples in Python
- Compare tuples in Python
- Remove duplicate tuples from list of tuples in Python
- Flatten Tuples List to String in Python
- Python program to find Tuples with positive elements in a List of tuples