Splitting a nested list with a string value in python

Question:

How would I be able to make a list comprehension that concatenates the input lists with and 'X' string as the separator. Note that the expected output will be string. The code below does not work please modify it.

val = [['_', '_', '_', '_'], ['_', '_', '_', '_', '_']]
output = ' '.join([cols if c != len(row)-1 else cols + 'X' for row in enumerate(formatted_list) for c,cols in enumerate(row)])
print(output)

input:

val = [['_', '_', '_', '_'], ['_', '_', '_', '_', '_']]

output:

_ _ _ _ X _ _ _ _ _ 

input:

val = [['t', 'python'], ['2', '6', '7'], ['9', '5']]

output:

t python X 2 6 7 X 9 5
Asked By: edge selcuk

||

Answers:

test_lst = [['_', '_', '_', '_'], ['_', '_', '_', '_', '_']]
sep_inner, sep_outer = ' ', ' X '

res = sep_outer.join([sep_inner.join(lst) for lst in test_lst])
print(res)
Answered By: Sergey Shirnin