S.M
S.M

Reputation: 71

nested lists in python

I am trying to split the following list:

A_list = [[[1,2,3,4],[5,6]],          ==>[0]
          [[3,4,5],[2,3,5]],          ==>[1]
          [[5,8,9],[10,11]],          ==>[2]
          [[22,20],[5,7,8]]]          ==>[3]

to:

 x_list = [[1,2,3,4],[5,6],
           [5,8,9],[10,11]]

 y_list = [[3,4,5],[2,3,5] ,
           [22,20],[5,7,8]]

x_list has row [0] , row [2] and row [4] ....

y_list has row [1] , row [3] and row [5] ...

where each row has 2 lists

is there any way to do that?

thank you for help or hints

I should mention that 'A_list' in this question has been modified. It was as below:

A_list = [[1,2,3,4],[5,6],          ==>[0]
      [3,4,5],[2,3,5],              ==>[1]
      [5,8,9],[10,11],              ==>[2]
      [22,20],[5,7,8]]              ==>[3]

most of the posted answers were to the question before editing. Thanks for all the helpful answers.

Upvotes: 0

Views: 746

Answers (6)

Błotosmętek
Błotosmętek

Reputation: 12927

Contrary to Python TOOWTDI principle, there are several ways to do it. One possibility is:

x_list = [A_list[i] for i in range(len(A_list)) if i % 4 < 2]
y_list = [A_list[i] for i in range(len(A_list)) if i % 4 >= 2]

Another:

rows = [ A_list[i:i+4] for i in range(0, len(A_list), 4)]
x_list = [x for row in rows for x in row[0:2]]
y_list = [x for row in rows for x in row[2:4]]

Both the solutions above are for the original problem (before it was modified). For solutions to the problem as it stands now see here.

Upvotes: 1

tobias_k
tobias_k

Reputation: 82889

(This first part of this answer was written before the question was edited, changing the layout of A_list.) Your list does not have "rows". Instead, you want to assign the 1st and 2nd element to x_list, the 3rd and 4th to y_list, 5th and 6th to x_list again, and so on.

For this, you can enumerate the elements in your list and assign them to x_list and y_list based on their index modulo 4.

>>> x_list = [a for i, a in enumerate(A_list) if i % 4 < 2]
>>> x_list
[[1, 2, 3, 4], [5, 6], [5, 8, 9], [10, 11]]
>>> y_list = [a for i, a in enumerate(A_list) if i % 4 >= 2]
>>> y_list
[[3, 4, 5], [2, 3, 5], [22, 20], [5, 7, 8]]

Or, you could first create those rows by grouping the list in groups of two entries, and then using slice notation [::2] and [1::2] to extract the elements and flatten those lists again:

>>> rows = [A_list[i:i+2] for i in range(0, len(A_list), 2)]
>>> x_list = [x for g in rows[::2] for x in g]
>>> y_list = [y for g in rows[1::2] for y in g]

After you silently modified your question (Don't do that; you invalidated most existing answers!) you can now use my 2nd approach, except that your new A_list is now already what rows was there:

>>> x_list = [x for g in A_list[::2] for x in g]
>>> y_list = [y for g in A_list[1::2] for y in g]

Upvotes: 3

a_guest
a_guest

Reputation: 36239

You can use slicing syntax to extract the required items and then flatten the resulting sub-lists:

x_list = [item for sub in A_list[::2] for item in sub]
y_list = [item for sub in A_list[1::2] for item in sub]

Upvotes: 2

Jude Molloy
Jude Molloy

Reputation: 193

If you are assuming that there are only two columns in each row then you can do something like this:

A_list = [[1,2,3,4],[5,6],
      [3,4,5],[2,3,5],
      [5,8,9],[10,11],
      [22,20],[5,7,8]]

x_list = []
y_list = []

for x in range(len(A_list))[::4]:
    x_list.append(A_list[x])
    x_list.append(A_list[x + 1])
    y_list.append(A_list[x + 2])
    y_list.append(A_list[x + 3])

Note: This only works if the number of columns is two. If the number of columns is not always two or another set number this is a difficult task as the Python list

 x_list = [[1,2,3,4],[5,6],
       [5,8,9],[10,11]]

Is the same as

 x_list = [[1,2,3,4],[5,6],[5,8,9],[10,11]]

This will work for any amount of inputs.

Upvotes: 0

Ma0
Ma0

Reputation: 15204

You can achieve this with slicing alone and no conditionals like so:

A_list = [[1,2,3,4],[5,6],
          [3,4,5],[2,3,5],
          [5,8,9],[10,11],
          [22,20],[5,7,8]]

x_list = A_list[::4] + A_list[1::4]
y_list = A_list[2::4] + A_list[3::4]
print(x_list)  # [[1, 2, 3, 4], [5, 8, 9], [5, 6], [10, 11]]
print(y_list)  # [[3, 4, 5], [22, 20], [2, 3, 5], [5, 7, 8]]

But it does not preserve the order. If that is important to you go with one of the other solutions posted.

Upvotes: -1

Just a simple loop.

A_list = [[1,2,3,4],[5,6],[3,4,5],[2,3,5],[5,8,9],[10,11],[22,20],[5,7,8]]
x_list = []
y_list = []
V = 0
for A in A_list:
    if V % 4 < 2:
        x_list.append(A)
    else:
        y_list.append(A)
    V = V + 1

print(x_list, y_list)

Upvotes: 1

Related Questions