I am currently working with tuples in Python an came upon this problem:
I have a tuple that looks like this:
[("mother", ["Mutter"]), ("and", ["und"]), ("father", ["Vater"]), ("I", ["ich", "mich"]),("not", ["nicht"]), ("at", ["dort", "da"]), ("home", ["Haus", "Zuhause"]), ("now", ["jetzt"])]
i need to create two lists, one, where the englisch words are stored and one, where the German words are stored, but I don't now how, since sometimes there are two German words for one English word.
Is there a nicer way than manually counting through the tuple and storing the words like that?
Thank you for your help!
CodePudding user response:
Heres a quick and easy way to do it:
wordlist = [("mother", ["Mutter"]), ("and", ["und"]), ("father", ["Vater"]), ("I", ["ich", "mich"]),("not", ["nicht"]), ("at", ["dort", "da"]), ("home", ["Haus", "Zuhause"]), ("now", ["jetzt"])]
english = []
german = []
for pair in wordlist:
english.append(pair[0])
for item in pair[1]: german.append(item)
print(english)
print(german)
CodePudding user response:
x = [("mother", ["Mutter"]), ("and", ["und"]), ("father", ["Vater"]), ("I", ["ich", "mich"]),("not", ["nicht"]), ("at", ["dort", "da"]), ("home", ["Haus", "Zuhause"]), ("now", ["jetzt"])]
english, german = [], []
for a, b in x:
english.append(a)
german.extend(b)
print(english, german)
# ['mother', 'and', 'father', 'I', 'not', 'at', 'home', 'now'] ['Mutter', 'und', 'Vater', 'ich', 'mich', 'nicht', 'dort', 'da', 'Haus', 'Zuhause', 'jetzt']