-2

So I have some arrays, which look like this:

x = ['1', '2', '3', etc]
y = ['1', '2', '3', etc]

What I want to do is when I have a list of lists, so say

z = [x, y]

I want to then iterate through z to obtain the n-th element of each list, as a float, and place it in an array.

So the desired output would be:

a = [1, 1]
b = [2, 2] 

and so on.

Is there a way to do this within a for loop?

MOA
  • 349
  • 1
  • 16

1 Answers1

1

Absolutely there is!

x = [1, 2, 3]
y = [1, 2, 3]
z = [x, y]

a = []
b = []
for items in z:
    a.append(items[0])
    b.append(items[1])

print(a)
print(b)

This will result in:

[1.0, 1.0]
[2.0, 2.0]

What that code does is go through a for loop and then checks the elements of the sub lists and appends each element index to a specific list.

Updated to meet requirement of N number of lists:

x = [1, 2, 3]
y = [1, 2, 3]

c = []
for i, j in zip(x, y):
    c.append([i, j])

for item in c:
    print(item)
Treatybreaker
  • 776
  • 5
  • 9