

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Reform K digit elements
When it is required to reform K digit elements, a list comprehension and the ‘append’ method are used.
Example
Below is a demonstration of the same
my_list = [231, 67, 232, 1, 238, 31, 793] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) temp = ''.join([str(ele) for ele in my_list]) my_result = [] for index in range(0, len(temp), K): my_result.append(int(temp[index: index + K])) print("The resultant list is :") print(my_result)
Output
The list is : [231, 67, 232, 1, 238, 31, 793] The value of K is 3 The resultant list is : [231, 672, 321, 238, 317, 93]
Explanation
A list is defined and is displayed on the console.
The value for K is initialized and is displayed on the console.
A list comprehension is used to iterate over the elements in the list and convert it to a string type, and join it by spaces.
This is assigned to a variable.
An empty list is defined.
The value up to K is iterated over and the elements from index 0 to K is appended to the empty list.
This is the output which is displayed on the console.
- Related Questions & Answers
- Extract tuples having K digit elements in Python
- Python – Average of digit greater than K
- Python – K middle elements
- Python program to extract Mono-digit elements
- Python – Split Numeric String into K digit integers
- Top K Frequent Elements in Python
- Python Program for Smallest K digit number divisible by X
- Program to find k where k elements have value at least k in Python
- Python – Sort by a particular digit count in elements
- Delete elements with frequency atmost K in Python
- Python – Next N elements from K value
- Python – Random insertion of elements K times
- Python program to find Non-K distant elements
- Largest K digit number divisible by X in C++
- Maximum and Minimum K elements in Tuple using Python
Advertisements