I want a Python program to import a list of words from a text file and print out the content of the text file as two lists. The data in the text file is on this form:
A Alfa B Betta C Charlie
I want a Python program to print out one list with A,B,C and one with Alfa, Betta, Charlie.
This is what I’ve written:
english2german = open('english2german.txt', 'r') englist = [] gerlist = [] for i, line in enumerate(english2german): englist[i:], gerlist[i:] = line.split()
This is making two lists, but will only print out the first letter in each word. How can I make my code to print out the whole word?
You want something like this:
The problem with your code before is that
englist[i:]is actually a slice of a list, not just a single index. A string is also iterable, so you were basically stuffing a single letter into several indices. In other words, something likegerlist[0:] = 'alfa'actually results ingerlist = ['a', 'l', 'f', 'a'].