At some point, you will need to read the entire file if you do not know in advance how many prefixes and suffixes are. Since I assume that you will not do this, or that it may change, and you do not want to maintain a number to store it, you will have to read the file, and readline () is a good way to do this.
However, you can pre-process your text file so that it uses a different format, such as a pickle file. In other words, read the text file in the dictionary and compile this dictionary. A dictionary might look something like this:
dic = {'prefixes': ['Ae' ,'di', ...], 'suffixes': ['dar', 'kil', ...]}
Using the length of the arrays, you can determine what the maximum random number is. It should be more efficient than reading the entire row of a row for a row every time. And if not, at least it's a slightly more elegant solution.
source share