0

I have multiple lists, for example,

x = [1,2,3,4]
y = [5,6,7,8]
z = [9,10,11,12]

And I want to write one element of each list to a file.

The output I'm trying to get is 1 5 9 2 6 10 3 7 11 4 8 12. So all the first elements, then all the second, and so forth.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
79t97g
  • 105
  • 1
  • 1
  • 9
  • What have you tried? Can you show your code please? Also, are you assuming all lists are the same size? – idjaw Mar 11 '16 at 02:26
  • 1
    ​​​​​​​​​​​​​​​`zip(x, y, z)`? Have you tried search your question before asking? – Remi Guan Mar 11 '16 at 02:26
  • I've tried nested for loops, but that doesn't work since it repeats it way too many times. Like `for i in range(len(x)): and for j in range(len(y)) and k in range(len(x))` Also, no the lists are not the same size (not necessarily) – 79t97g Mar 11 '16 at 02:26
  • `write_file.write(" ".join(x for tup in zip(x, y, z) for x in tup))` – zondo Mar 11 '16 at 02:28
  • Is there always 4 items per list? – arcee123 Mar 11 '16 at 02:28

3 Answers3

1

Use Zip and convert each integer element to string so that you can join them:

x = [1,2,3,4]
y = [5,6,7,8]
z = [9,10,11,12]

g = " ".join(str(x) for t in zip(x, y, z) for x in t)

Write g to your file.

Excerpt on how Zip works:

When you zip() together M lists containing N elements each, the result has N elements. Each element is a M-tuple.

In other words, zip(x,y,z) changes your:

[1,2,3,4]
[5,6,7,8]
[9,10,11,12]

to

[(1,5,9),(2,6,10),(3,7,11),(4,8,12)]
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
0

This should give you the ability to have three lists that might have different number of items in each one.

number = max(x.len(), y.len(),z.len())
count = 0
total = {}
while count < number:
     try:
           total.append(x[count])
     except:
           pass
     try:
           total.append(y[count])
     except:
           pass
     try:
           total.append(z[count])
     except:
           pass
     count += 1
print(' '.join(total))
arcee123
  • 101
  • 9
  • 41
  • 118
  • Why not use `itertools.zip_longest()`? – zondo Mar 11 '16 at 02:39
  • because I'm not that educated. I'll have to go dig it about. – arcee123 Mar 11 '16 at 02:40
  • Don't worry. I didn't know about until this month :) – zondo Mar 11 '16 at 02:41
  • It is good that your solution can handle multiple lists with different number of items. However, if you just remove the try-catch and doing pre-check before adding the element, the solution would be a lot better – Ian Mar 11 '16 at 02:42
  • is it faster to run a If vs a try? I always thought a try was faster. – arcee123 Mar 11 '16 at 02:42
  • `try` is OK, but `except` is not. If your lists contain different number of items, then it is expected that it will have many `excepts` – Ian Mar 11 '16 at 02:44
0

In case you don't have lists of equal lengths, you can use zip_longest (or izip_longest in python 2):

from itertools import zip_longest

x = [1, 2, 3, 4, 1]  
y = [5, 6, 7, 8, 99]
z = [9, 10, 11, 12]  # short list

reordered = []
for elements in zip_longest(x, y, z):
    reordered.extend(elements)

Results in a list of:

[1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12, 1, 99, None]

monkut
  • 42,176
  • 24
  • 124
  • 155