Home > Net >  Merge two list to create a table in a txt file in python
Merge two list to create a table in a txt file in python

Time:10-16

I want to merger these two list in one list that should look like this :

newlist = ['Salle 13\tBullet, Train Carter\n','Salles 06\t Sita Ramam, L’année du requin\n'...]

so each "salle" will have two names Here the two list needed to merge

ordersalles = ['Salle 13', 'Salle 06', 'Salle 12', 'Salle 10', 'Salle 11', 'Salle 08', 'Salle 07', 'Salle 01', 'Salle 04', 'Salle 09', 'Salle 03', 'Salle 05', 'Salle 02']
orderfilms = ['Bullet train', 'Carter', 'Sita Ramam', 'L’année du requin', 'Les Promesses d’Hasan', 'Luck', 'Ménestrel', 'Wedding Season', 'Poikkal Kudhirai', 'Des feux dans la nuit', 'The Bikeriders', 'Darling', 'Treize vies', 'En décalage', 'Night Raiders', 'The Last Son', 'Embuscade', 'Mission Eagle', 'Le Destin des Tortues Ninja, le Film', 'Amants Super-héroïques', 'Doblemente Embarazada', 'Alina of Cuba', 'Prey', 'La Vie en plus grand', 'Hero Mode']

I have tried to do something with two for loop like this :

newlist = []
for i in ordresalles:
    for e in ordre films:
        newlist.append(i)
        newlist.append(e)

One salle, the last one, should not have two names but only one as there is 13 salles and 25 names If someone can help me for this please

CodePudding user response:

You can create the list using f-strings and zipping the lists;

newlist = [f'{os}\t{of1}, {of2}' for os, of1, of2 in zip(ordersalles, orderfilms[::2], orderfilms[1::2])]

Adding in writing the list to a file:

with open('out.txt', 'w') as f:
    f.write('\n'.join(newlist))
  • Related