Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combine elements of a list of lists into a string

I have to combine these 2 lists:

dots = [['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.', '.']]
spaces = [['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   '],
 ['   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ', '   ']]

I want every dot separeted by a space and turn them into string just like this:

.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .

I wrote this code but it only work for the first line:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lists = [a for b in zip(dots[0], spaces[0]) for a in b]
line = ''.join(liste)

I wanted to know how to loop in for every other sublist in these two lists.

>Solution :

You know how to use zip to simultaneously iterate over two lists, so do that and create a list of strings containing a dot and space for each "row":

lines = [
          "".join(
              dot + space 
              for dot, space in zip(row_dots, row_spaces)
          )
          for row_dots, row_spaces in zip(dots, spaces) 
       ]

Then join the individual lines using the newline character '\n'.

output = "\n".join(lines)
print(output)

.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .
.   .   .   .   .   .   .   .   .

Of course, you can combine both into a single statement so that you don’t need to create the list of lines:

output = "\n".join(
              "".join(
                  dot + space 
                  for dot, space in zip(row_dots, row_spaces)
              )
              for row_dots, row_spaces in zip(dots, spaces) 
         )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading