Merge two list to create a table in a txt file in python

Question:

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

newlist = ['Salle 13tBullet, Train Cartern','Salles 06t Sita Ramam, L’année du requinn'...]

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

Asked By: Az7

||

Answers:

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))
Answered By: bn_ln
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.