-1

How can I convert a list of arrays into one single list and print 6 elements per line?

the list of arrays looks like this:

[['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
 ['SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
 ['MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n'],
 ['PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']]

and I want it to look like this:

['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n',
 'SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n',
 'MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n',
 'PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']

what I have tried is this code:

merged_list = []

for l in mylist:
    merged_list += l

and this is what I got:

['UES9151GS5',
 'SQU6245R',
 'DEN',
 'FRA',
 '1420564460',
 '1049\n',
 'SJD8775RZ4',
 'SQU6245R',
 'DEN',
 'FRA',
 '1420564460',
 '1049\n',
 'MXU9187YC7',
 'MOO1786A',
 'MAD',
 'FRA',
 '1420563408',
 '184\n',
 'PUD8209OG3',
 'XXQ4064B',
 'JFK',
 'FRA',
 '1420563917',
 '802\n']

What I am looking for is to print 6 elements per line Thank you very much for your help.

glibdud
  • 7,550
  • 4
  • 27
  • 37
s_am
  • 57
  • 1
  • 9
  • 5
    wait, im confused. you did it, right? what's the issue/question then? – Paritosh Singh May 22 '19 at 13:35
  • Possible duplicate of this [post](https://stackoverflow.com/questions/29244286/how-to-flatten-a-2d-list-to-1d-without-using-numpy) – Alexandre B. May 22 '19 at 13:35
  • 3
    Do you just want to print it 6 elements per line? – Error - Syntactical Remorse May 22 '19 at 13:37
  • @Error-SyntacticalRemorse I want to print 6 elements per line – s_am May 22 '19 at 13:38
  • @ParitoshSingh I want to print 6 elements per line – s_am May 22 '19 at 13:44
  • 1
    What is the purpose? Do you need it to still look like a python list, with the `[]` and commas? – glibdud May 22 '19 at 13:46
  • @glibdud yes, because I am going to combine it with another list that has the same order (6 lines per element) – s_am May 22 '19 at 13:48
  • 4
    That doesn't make sense, how you print it has nothing to do with how you combine it with another list, can you clarify what you want to do exactly? – Devesh Kumar Singh May 22 '19 at 13:50
  • I agree with Devesh. Printing is just for visual use. It has no role in manipulation. Maybe you should say what you actually want and you will get a more helpful answer. – Error - Syntactical Remorse May 22 '19 at 13:50
  • This question makes no sense, because the presentation of a FLAT list does not matter. You answered your own question. – Mr. Polywhirl May 22 '19 at 13:57
  • @DeveshKumarSingh, It does make sense I just tried and combined them and they didn't have the same order. what about if I want to write it into a CSV file it will not be written in the same order. – s_am May 22 '19 at 14:13
  • @Mr.Polywhirl It does make sense I just tried and combined them and they didn't have the same order. what about if I want to write it into a CSV file it will not be written in the same order. – s_am May 22 '19 at 14:13
  • @s.m.yousef I'm not sure what you mean. If you wrote your initial 2-d list to CSV, each line should be written to the file in the same order they appear in the list, and likewise each element of each line should maintain order. If they're not, then something else strange is going on that you're not showing here. – glibdud May 22 '19 at 14:16
  • @glibdud what do you mean by strange? I am just new to python and learning by myself. rather making assumption it would be better if you show me where is my mistakes, guide me and suggest some helpful material to read. anyway thank you – s_am May 22 '19 at 14:21
  • @s.m.yousef SO isn't really a tutorial site. It's best for specific answers to specific questions. In any case, we can't show you where your mistakes are because you haven't actually shown us any code beyond how you flattened the list (which is fine, if flattening the list is what you're going for). You might want to read [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), as I believe it's relevant here. – glibdud May 22 '19 at 14:26
  • @glibdud my question was specific and clear **How can I convert a list of arrays into one single list and print 6 elements per line?** . and who said that SO isn't a tutorial site. when you ask a question and people answer you, you learn something new – s_am May 22 '19 at 14:31
  • @s.m.yousef But when asked why you need to do this, you talked about combining it with another list and creating CSVs. Neither of those things are affected by how you print a list. – glibdud May 22 '19 at 14:36
  • @glibdud if I knew how to print it, I might be able to store it in a new list then combine it (I need to have it in a specific format) because each element should be written in a specific column (6 columns) – s_am May 22 '19 at 14:42
  • @s.m.yousef Well then, at least one of the answers you've already gotten gives you the format you appear to be asking for. So I guess you've got what you need? – glibdud May 22 '19 at 14:47
  • @glibdud I am testing them on my data. But apparently it works fine with printing but not with storing in a new list – s_am May 22 '19 at 14:49

5 Answers5

2

To unpack the original nested arrays into a single array:

a = [['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
     ['SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
     ['MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n'],
     ['PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']]

b = []
for row in a:
    b += row

Which results in:

b = ['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n', 'SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n', 'MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n', 'PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']

If you want to print the single array with linebreaks after each string containing "\n", then:

print(*b)

where the splat (*) denotes iterable unpacking. (see https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments)

Dave
  • 3,834
  • 2
  • 29
  • 44
  • 1
    Maybe need to explain it is equivalent to `print(a, b, c)` where the `*` unpacks the list. – knh190 May 22 '19 at 14:01
0

This will get your output:

from functools import reduce
d = [['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
 ['SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
 ['MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n'],
 ['PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']]
merged_list = reduce(lambda x, y: x + y, d)
print('[', end="")
for index, element in enumerate(merged_list):
    if element.endswith('\n'):
        if index != len(merged_list) - 1:
            print(f"'{element.strip()}\\n',\n", end="") # last element in row
        else:
            print(f"'{element.strip()}\\n'", end="") # last element in list
    else:
        print(f"'{element.strip()}', ", end="")
print(']')

Output:

['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n',
'SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n',
'MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n',
'PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']
0

One suggestion is to make sure to distinguish between data and its "representation". If you wish to only print 6 characters per line, it is no longer a question of how the data itself is stored, but rather one of representing the information correctly.

With that in mind, the simplest way is to simply print each row of data separately.

mylist = [['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
          ['SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
          ['MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n'],
          ['PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']]

for row in mylist:
    print(" ".join(row))

Output:

UES9151GS5 SQU6245R DEN FRA 1420564460 1049

SJD8775RZ4 SQU6245R DEN FRA 1420564460 1049

MXU9187YC7 MOO1786A MAD FRA 1420563408 184

PUD8209OG3 XXQ4064B JFK FRA 1420563917 802

If you notice, there is an extra newline at the end, which appears because your data had it in the last string for each row. You can just slice it off for a "quick fix"

for row in mylist:
    print(" ".join(row)[:-1])
#Output:
UES9151GS5 SQU6245R DEN FRA 1420564460 1049
SJD8775RZ4 SQU6245R DEN FRA 1420564460 1049
MXU9187YC7 MOO1786A MAD FRA 1420563408 184
PUD8209OG3 XXQ4064B JFK FRA 1420563917 802
Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
0

If you just want to merge two list, use the original list and use extend() to append the second list at the end:

merged_list = mylist 
merged_list.extend(second_list)
LaurinO
  • 136
  • 6
0

You should use the following function:

def flatten(iterable):
    for item in iterable:
        try:
            yield from flatten(item)
        except TypeError:
            yield item

If you are running the interactive interpreter, you could use a modified version of it like this:

>>> def flatten(iterable):
    for item in iterable:
        if isinstance(item, str):
            yield item
        else:
            try:
                yield from flatten(item)
            except TypeError:
                yield item


>>> array_2D = [['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
 ['SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n'],
 ['MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n'],
 ['PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']]
>>> flat_array = list(flatten(array_2D))
>>> flat_array
['UES9151GS5', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n', 'SJD8775RZ4', 'SQU6245R', 'DEN', 'FRA', '1420564460', '1049\n', 'MXU9187YC7', 'MOO1786A', 'MAD', 'FRA', '1420563408', '184\n', 'PUD8209OG3', 'XXQ4064B', 'JFK', 'FRA', '1420563917', '802\n']
>>> 
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117