Split a List having Single Integer - Python
Last Updated :
11 Jul, 2025
We are given a list containing a single integer, and our task is to split it into separate digits while keeping the list structure intact. For example, if the input is a = [12345], the output should be [1, 2, 3, 4, 5]. Let's discuss different methods to do this in Python.
Using List Comprehension
We can convert the integer to a string, iterate through its characters and convert them back to integers.
Python
# Initializing list
a = [12345]
# Splitting the integer into digits
result = [int(digit) for digit in str(a[0])]
print(result)
Explanation:
- str(a[0]) converts the integer to a string "12345".
- The list comprehension iterates through each character, converting it back to an integer.
Let's explore some more ways and see how we can split a list having single integer in Python.
Using map()
map() function can apply int() to each character of the string representation of the number.
Python
# Initializing list
a = [12345]
# Splitting the integer using map()
result = list(map(int, str(a[0])))
print(result)
Explanation:
- str(a[0]) converts the integer to a string.
- map(int, str(a[0])) applies int() to each digit.
- list() converts the result back to a list.
Using for Loop and Integer Division
We can extract digits manually by repeatedly dividing the number by 10.
Python
# Initializing list
a = [12345]
# Extracting digits using integer division
num = a[0]
result = []
while num > 0:
result.append(num % 10) # Extract last digit
num //= 10 # Remove last digit
result.reverse() # Reverse to maintain correct order
print(result)
Explanation:
- The loop extracts digits from right to left using num % 10.
- num //= 10 removes the last digit after extracting it.
- Since digits are added in reverse order, result.reverse() ensures the correct order.
- This method does not use string conversion but is slightly slower than string-based methods.
Using divmod() for Better Integer Division
divmod() function simplifies integer division and remainder extraction.
Python
# Initializing list
a = [12345]
# Extracting digits using divmod()
num = a[0]
result = []
while num > 0:
num, digit = divmod(num, 10) # Get quotient and last digit
result.append(digit)
result.reverse()
print(result)
Explanation:
- divmod(num, 10) returns both the quotient and last digit in one step.
- This avoids separate division and modulo operations, making the method more efficient than a standard loop.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice