Change language

Python | Split a list that has a single integer

| | |

Examples :

  Input:  Input = [23]  Output:  Output = [2, 3]  Input:  Input = [15478]  Output:  Output = [1, 5, 4, 7, 8] 

Method # 1: Using cards

# Python code to split a list containing a single integer

  
# Initialization list

input = [ 200 ]

 
# Using the map

output = list ( m ap ( int , str ( input [ 0 ])))

 
# Printout

print (output)

Exit:

 [2, 0, 0] 

Method # 2: Using List Comprehension

# Python code to split the list, containing one integer

 
# Initialization list

input = [ 231 ]

  
# Using the comprehension list

output = [ int (x) if x.isdigit () else

  for z in input for x in str (z)]

 
# Printout

print ( output)

Exit:

 [ 2, 3, 1] 

Method # 3: Using Loops

# Python code to split the list containing one integer

 
# Initialization list

input = [ 987 ]

  
# Convert to int

input = int ( input [ 0 ])

  
# Initialize the output list

output = []

 

while input " 0 :

  rem = input % 10

input = int ( input / 10 )

output.append (re m)

 
# Printout

print (output)

Exit:

 [7, 8, 9] 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method