Python: Combined lists iteration
NickName:Benjamin BB Ask DateTime:2018-05-05T18:20:18

Python: Combined lists iteration

I've got two lists:

synonymes = ['verb1', 'verb2', 'verb3']

phrases = ['blabla verb blabla', 'blibli verb blibli', 'verb bloblo bloblo']

and I would like to combine them to obtain this result in another list:

liste = ['blabla verb1 blabla','blabla verb2 blabla', ... 'verb3 bloblo bloblo']

the following code works but it's not very elegant, and I would like to give the 2 lists as entries of the function that's doing this job, which is impossible with this solution:

while j < len(synonymes):
    liste.append("blabla "+synonymes[j]+" blabla")
    liste.append("blibli "+synonymes[j]+" blibli")
    liste.append(synonymes[j]+" bloblo bloblo")
    j+=1

I can change the structure of these two lists if need be, as long as I can obtain this final list

What would be a proper way to do so in python?

Thanks!

EDIT : to make myself clearer, here is the exact output that I want:

["blabla verb1 blabla", "blabla verb2 blabla", "blabla verb3 blabla", "blibli verb1 blabla", "blibli verb2 blibli", "blibli verb3 blibli", "verb1 bloblo bloblo", "verb2 bloblo bloblo", "verb3 bloblo bloblo"]

With the length of synonymes and phrases both variable.

Copyright Notice:Content Author:「Benjamin BB」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50188344/python-combined-lists-iteration

Answers
Austin 2018-05-05T10:23:50

Use zip to pair and replace to replace:\n\n>>> synonymes = ['verb1', 'verb2', 'verb3']\n>>> phrases = ['blabla verb blabla', 'blibli verb blibli', 'verb bloblo bloblo']\n>>> [y.replace('verb', x) for x, y in zip(synonymes, phrases)]\n['blabla verb1 blabla', 'blibli verb2 blibli', 'verb3 bloblo bloblo'] \n\n\nEDIT:\n\n>>> [y.replace('verb', x) for y in phrases for x in synonymes]\n['blabla verb1 blabla', 'blabla verb2 blabla', 'blabla verb3 blabla', 'blibli verb1 blibli', 'blibli verb2 blibli', 'blibli verb3 blibli', 'verb1 bloblo bloblo', 'verb2 bloblo bloblo', 'verb3 bloblo bloblo'] \n",


Mehrdad Pedramfar 2018-05-05T10:24:53

Try this:\n\nfor i in enumerate(phrases):\n phrases[i[0]] = phrases[i[0]].replace('verb', synonymes[i[0]])\n\n\nor in one line: \n\n[phrases[i[0]].replace('verb', synonymes[i[0]]) for i in enumerate(phrases)]\n",


Fanto 2018-05-05T10:29:26

Using the same format used in your code\n\nliste = [phrase.format(verb=verb) for verb, phrase in zip(synonymes, phrases)]\n\n\nbut for make this you have to change the phrases list like this:\n\nphrases = ['blabla {verb} blabla','blabla {verb} blabla', ... '{verb} bloblo bloblo']\n",


More about “Python: Combined lists iteration” related questions

Python: Combined lists iteration

I've got two lists: synonymes = ['verb1', 'verb2', 'verb3'] phrases = ['blabla verb blabla', 'blibli verb blibli', 'verb bloblo bloblo'] and I would like to combin

Show Detail

Python Nested lists and iteration

I am new to Python and I did my search but I could not find what I am looking for. I apologise in advance if this question has been asked and if I could not find it due to my lack of not knowing th...

Show Detail

How to print an item with least combined rank in two lists

I have just started with Python and trying to solve this problem. Given the two lists, I have to print the item with the least combined rank in the two lists. Here is my approach so far, but it see...

Show Detail

iteration of a variable over 2 lists in python using for loop

What happens in the case of a following for loop in python? How will the values of i change in each iteration. Does the sequences x and y combine to form a single sequence? for i in x, y:

Show Detail

Updating list of lists at iteration

I've a list of lists and I want to update each list at iteration. I initialized my list as follows: my_list = [[0]*n]*n When I want to update the inner lists, by something like: for i in range(

Show Detail

What is Inline Iteration in Python?

&quot;What is Inline Iteration?&quot; I tried googling this question but all the results are about List Comprehension. I thought it's the same thing until I read this comment: &quot;List comprehens...

Show Detail

product of two lists in python

How to combine 2 lists in such a way that each element in list 2 is combined with the elements in list 1 so that “a” combines with only one element in list 1 in each iteration then “b” combines wit...

Show Detail

Create combined Lists from multiple Lists

I can't quite get my head around trying to figure this out but I'll explain as follows, var combinedCoords = new List&lt;List&lt;int&gt;&gt;(); var coords = new List&lt;List&lt;int&gt;&gt; {

Show Detail

Python restoring an interrupt iteration of two lists

Here the code: import itertools # import list from file a = [line.strip() for line in open("real.txt", 'r')] b = [line.strip() for line in open("imag.txt", 'r')] # create file contain r

Show Detail

Randomizing two lists and maintaining order in Python 3.4

I'm basically asking the exact same question as was asked here, but for Python 3.4.0. In 3.4.0, this code: a = ["Spears", "Adele", "NDubz", "Nicole", "Cristina"] b = [1, 2, 3, 4,

Show Detail