0

I have a complex list of lists that looks like that :

[[['MARIA DUPONT',
   ' infos : ',
   [' age = 28',
    ' yeux = bleus',
    ' sexe = femme']],
  [' + ']],
 [['PATRICK MARTIN',
   ' infos : ',
   [' age = 53',
    ' yeux = marrons',
    ' sexe = homme']],
  [' + ']],
 [['JULIE SMITH',
   ' infos : ',
   [' age = 17',
    'yeux = verts',
    'sexe = femme']],
  [' fin ']]]

I am trying to transform it into a string. At the end I want to print that :

MARIA DUPONT,
 infos :
 age = 28 
 yeux = bleus 
 sexe = femme 

 + 

PATRICK MARTIN
 infos :
 age = 53
 yeux = marrons
 sexe = homme

 + 

JULIE SMITH
 infos :
 age = 17
 yeux = verts
 sexe = femme

 fin

My real data are more complicated and I have lists into level 5. So I am looking for a way to solve the problem I explained to be able to adapt it and apply it to my real data.

I am trying with ''.join(list) and ''.join(x for x in list)

But in both cases I have the error TypeError: list indices must be integers or slices, not list

I've tryed other ways but now I'm confused and I didn't found a good solution to reach my goal.

Any help would be appreciated, and thanks in advance. (and sorry for my bad english!)

4 Answers 4

1

You can use str.join with a single pass over the lists:

data = [[['MARIA DUPONT', ' infos : ', [' age = 28', ' yeux = bleus', ' sexe = femme']], [' + ']], [['PATRICK MARTIN', ' infos : ', [' age = 53', ' yeux = marrons', ' sexe = homme']], [' + ']], [['JULIE SMITH', ' infos : ', [' age = 17', 'yeux = verts', 'sexe = femme']], [' fin ']]]
r = '\n'.join('\n'.join([a, b, *c, f'\n{k}\n']) for [a, b, c], [k] in data)

Output:

MARIA DUPONT
 infos : 
 age = 28
 yeux = bleus
 sexe = femme

 + 

PATRICK MARTIN
 infos : 
 age = 53
 yeux = marrons
 sexe = homme

 + 

JULIE SMITH
 infos : 
 age = 17
 yeux = verts
 sexe = femme

 fin 

If your lists are arbitrarily nested, then you can use recursion with a generator:

def flatten(d):
  if isinstance(d, str):
     yield d
  else:
     yield from [i for b in d for i in flatten(b)]

print('\n'.join(flatten(data)))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that is exactly what I needed, I applied it into my real data and it works perfectly !
1

.join() won't work with a list in the list. I can offer you a solution based on recursion.

def list_to_str(_list):
    result = ""
    if isinstance(_list, list):   
        for l in _list:
            result += list_to_str(l)
    else:
        result += _list
    return result

result_string = list_to_str(your_list)

print(result_string)

Comments

1

I can't tell if you have a list with varying levels of lists but if so, you would probably need a conditional to see if the list goes further and recursively iterate the list.

def convert_list(dataset):
    result = ''
    for element in dataset:
        if isinstance(element, list):
            result += convert_list(element)
        else:
            result += str(element)
    return result

This will not print the newlines you want but it does return the list as a string.

Comments

1

Write a recursive function to get inside your lists like below:

def print_data(input_list):

    for obj in input_list:
        if isinstance(obj, list):
            print_data(obj)
        else:
            print(obj)



input_list = [[['MARIA DUPONT',
                    ' infos : ',
                    [' age = 28',
                     ' yeux = bleus',
                     ' sexe = femme']],
                   [' + ']],
                  [['PATRICK MARTIN',
                    ' infos : ',
                    [' age = 53',
                     ' yeux = marrons',
                     ' sexe = homme']],
                   [' + ']],
                  [['JULIE SMITH',
                    ' infos : ',
                    [' age = 17',
                     'yeux = verts',
                     'sexe = femme']],
                   [' fin ']]]
print_data(input_list)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.