How to split integer and add to list?

Question:

I want to know how can we program in Python please.

So,
D = read().value

And D value is updated two times
First time D = [50, 11, 18, 21, 34]
Second time D = 1346 (second time D value is always 4 digit integer)

I want to get the final result like this (First time D array with the second time D with 4 digit integer has to be split into two elements and added to the end of the array)

D = [50, 11, 18, 21, 34, 13, 46]

Asked By: elisa thomas

||

Answers:

You can achieve this result with by using a temporary variable for storing the initial list of numbers contained in D when D is updated to contain an integer. Then you can cast the integer to a string and use substrings to get the first two characters of the string and then cast them back to and integer. Then you append the integer to the list of integers contained in the temporary variable. Repeat for the last two characters in of the string representation of the four digit number contained in D. Finally we assign the value of the temporary variable back to D and print it out. This code should behave as I outlined:

D = [50, 11, 8, 21, 4]
print(D)
tmp = D
D = 1346
D = str(D)
tmp.append(int(D[:2]))
tmp.append(int(D[2:]))
D=tmp
print(D)
Answered By: Erik Wiström
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.