0

I have a string of plot ploints such as:

plots = [(0, 1), (0, 2), (1, 4), ... (0.4, 0.8) etc

But I want to split them into respective x and y's:

x = (0, 0, 1, ... 0.4)
y = (1, 2, 4, ... 0.8)

I am unsure how to do this.

Olvin Roght
  • 6,675
  • 2
  • 14
  • 32
Bronica
  • 9
  • 1

3 Answers3

2

python provides you with the zip() function

x, y = zip(*plots)

which would provide your exact need, the asterisk would unpack the list into tuples, than the zip function "zips" the tuples together, that is creates a tuple for each index of the tuples

Rotem Tal
  • 731
  • 4
  • 11
1
plots = [(0, 1), (0, 2), (1, 4), (0.4, 0.8)]

x = [plot[0] for plot in plots]
y = [plot[1] for plot in plots]
Artyom Vancyan
  • 2,348
  • 3
  • 10
  • 28
0

This could be an inline solution

plots = [(0, 1), (0, 2), (1, 4)]
x, y = [item[0] for item in plots], [item[1] for item in plots]
Raihan Kabir
  • 412
  • 2
  • 9