0

the teacher give me a number sequence 17 23 39 43 55 61 70 83 (these numbers follow no rule, just raw input) to input:

box = list(map(int, input().split()))
# or
box = [int(x) for x in input().split()]

how can i create a list like this using both list comprehension and do the input().split() at once?, is it possible?:

[(17, 23), (39, 43), (55, 61), (70, 83)]

i have tried this:

A = [int(i1) for i1 in input().split()]
A[:] = [tuple([A[i2],A[i2+1]]) for i2 in range(0,len(A),2)]
print(A)

# i don't know if this code is optimized, if it is not optimized it would be a pleasure to me if you rewrite this code :D
17 23 39 43 55 61 70 83 #input
[(17, 23), (39, 43), (55, 61), (70, 83)] #output

as you can see, in line1 all i do is input those int(number) into a list

at line2, i use 2d array + list comprehension to convert them to [(17, 23), (39, 43), (55, 61), (70, 83)]

is it possible to do both input() and convert them to [(17, 23), (39, 43), (55, 61), (70, 83)] in just 1 line?

mozway
  • 194,879
  • 13
  • 39
  • 75

0 Answers0