-2
  1. ['4', '5', '6', '8', '5', '9', '6', '2', '6', '5']

     How can I get it to look like this:
    
  2. [45,68,59,62,65]

How to combine two or more list elements like ['4','5'] to [45]

1 Answers1

1

The strategy is to pair the initial data, then convert to int, zip and slices are a good way dot that

values = ['4', '5', '6', '8', '5', '9', '6', '2', '6', '5']
result = [int(a + b) for a, b in zip(values[::2], values[1::2])]
print(result)  # [45, 68, 59, 62, 65]

  • values[::2] : one over two ['4', '6', '5', '6', '6']
  • values[1::2] : over over two, starting at 2nd ['5', '8', '9', '2', '5']
azro
  • 53,056
  • 7
  • 34
  • 70
  • Or using indexes `[int("".join(values[i: i + 2])) for i in range(0, len(values), 2)]` – Olvin Roght Nov 10 '21 at 07:39
  • @OlvinRoght I won't verify but `[int(values[i] + values[i + 1]) for i in range(0, len(values), 2)]` maybe faster – azro Nov 10 '21 at 07:42