Forexample, if i were that have an array such as i = [1,2,3,4,5,6,7,8,9] I want to separate them into i = [123,456,789]
Asked
Active
Viewed 19 times
-2
-
1What have you tried? Can you share example code? – urban Jun 25 '20 at 11:36
-
2Does this answer your question? [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – user120242 Jun 25 '20 at 11:37
1 Answers
0
You could do something like this:
l = [1,2,3,4,5,6,7,8,9]
# set the number of numbers that you want to have "concatenated"
num = 3
# initialise a new list
l_bis = []
# iterate over each pieces of the list that contain three digits
for i in range(len(l)//num):
# append the new integer after having joined strings of the original integers
l_bis.append(int("".join([str(x) for x in l[i*num:i*num+num]])))
print(l_bis)
# [123,456,789]
Please note that this approach will only work when the length of the initial list is a multiple of the variable num
!

bglbrt
- 2,018
- 8
- 21