czy
czy

Reputation: 513

How to put the element in the same position of two lists together to form a list of lists?

I have two lists now shown below:

Coord_leaking_pipes_x_coords:  [387.02, 1899.68, 2485.55, 1167.79, 296.91, 937.42, 1293.7, 267.81, 540.25, 1112.86, 1249.76, 1655.71, 2413.3, 2691.82]
Coord_leaking_pipes_y_coords:  [956.05, 803.57, 632.88, 713.02, 1569.97, 1141.56, 960.69, 423.37, 317.17, 345.85, 430.96, 657.27, 397.27, 842.85]

I want to get the new list shown below, it is to put the element in the same position of two lists shown above together to form a list and put those new lists into a big list.

Coord_leaking_pipes_coords; [[387.02,956.05],[1899.68,803.57],[2485.55,632.88],[1167.79,713.02],......,[2691.82,842.85]]

Could you please tell me how can I achieve it?

Upvotes: 0

Views: 751

Answers (2)

gesling
gesling

Reputation: 77

x = [387.02, 1899.68, 2485.55, 1167.79, 296.91, 937.42, 1293.7, 267.81, 540.25, 1112.86, 1249.76, 1655.71, 2413.3, 2691.82]

y = [956.05, 803.57, 632.88, 713.02, 1569.97, 1141.56, 960.69, 423.37, 317.17, 345.85, 430.96, 657.27, 397.27, 842.85]

result = [[x[i],y[i]] for i in range(len(x))]

Upvotes: 0

martineau
martineau

Reputation: 123473

Here's one way that uses the built-in zip() function:

x_coords = [387.02, 1899.68, 2485.55, 1167.79, 296.91, 937.42, 1293.7, 267.81, 540.25, 1112.86, 1249.76, 1655.71, 2413.3, 2691.82]
y_coords = [956.05, 803.57, 632.88, 713.02, 1569.97, 1141.56, 960.69, 423.37, 317.17, 345.85, 430.96, 657.27, 397.27, 842.85]

coords = list(list(t) for t in zip(x_coords, y_coords))

Upvotes: 1

Related Questions