I am newbie with Python and I am trying to solve this problem in a script.
I have 2 lists of dictionaries as follows:
en_list = [{'time': 840, 'text': "I want to introduce you to some\nvery wise kids that I've known,"},
{'time': 5480, 'text': 'but first I want\nto introduce you to a camel.'},
{'time': 8720, 'text': 'This is Cassie, a therapy camel\nvisiting one of our young patients'},
{'time': 13000, 'text': 'in her room,'},
{'time': 14920, 'text': 'which is pretty magical.'}]
fr_list = [{'time': 840, 'text': "Je veux vous présenter certains enfants\ntrès sages que j'ai rencontrés,"},
{'time': 5480, 'text': 'mais je veux commencer\npar vous présenter un chameau.'},
{'time': 8720, 'text': 'Voici Cassie, un chameau thérapeutique qui\nrend visite à une de nos jeunes patients'},
{'time': 14920, 'text': 'ce qui est plutôt magique.'}]
I want to create a new list with only matching values of the 'time' key.
I came up with this, but apparently it doesn't take the time key into consideration although it works just fine if both lists are having the same number of dictionaries.
for i, m in enumerate(zip(en_list, fr_list), start=1):
print(i, m[0], "=", m[1])
This prints out the following:
1 {'time': 840, 'text': "I want to introduce you to some\nvery wise kids that I've known,"} = {'time': 840, 'text': "Je veux vous présenter certains enfants\ntrès sages que j'ai rencontrés,"}
2 {'time': 5480, 'text': 'but first I want\nto introduce you to a camel.'} = {'time': 5480, 'text': 'mais je veux commencer\npar vous présenter un chameau.'}
3 {'time': 8720, 'text': 'This is Cassie, a therapy camel\nvisiting one of our young patients'} = {'time': 8720, 'text': 'Voici Cassie, un chameau thérapeutique qui\nrend visite à une de nos jeunes patients'}
4 {'time': 13000, 'text': 'in her room,'} = {'time': 14920, 'text': 'ce qui est plutôt magique.'}
As you can see, it wrongly mapped the English with 'time': 13000
to the French with 'time': 14920
although the English list has the correct text with the same time, but the code above ignored it.
The desired output should include all items with the matching value of 'time' key and ignore the non-matching items. How can I achieve this?
Thanks in advance for your support!