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
  • 7,677
  • 2
  • 16
  • 35
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
  • 739
  • 5
  • 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
  • 5,029
  • 3
  • 12
  • 34
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
  • 447
  • 2
  • 10