-1

I have my first question here. I'm writing a code in Python. I have following list

X = [0.2,0.3,0.2,0.3,0.2,0.3,0.2,0.3,0.2,0.3]

I would like to create a new list made by tuples of two or a different number of elements of the first list as follows.

newList = [(0.2,0.3),(0.2,0.3),(0.2,0.3),(0.2,0.3),(0.2,0.3)]

I'm looking for a function of the lists or tuples. or maybe something easy, without the use of a big "For"

1 Answers1

2

Use map and lambda:

>>> n = 2
>>> list(map(lambda i: tuple(X[i: i+n]), range(0, len(X), n)))
>>> [(0.2, 0.3), (0.2, 0.3), (0.2, 0.3), (0.2, 0.3), (0.2, 0.3)]
akash karothiya
  • 5,736
  • 1
  • 19
  • 29