Home > Enterprise >  How would I append a tuple to a list with a loop?
How would I append a tuple to a list with a loop?

Time:10-13

I'm trying to append a tuple to a list with a loop but can't seem to figure it out. I feel that I am very close as i'm getting an index error - list index out of range.

I've tried adding or subtracting in the WordListToTuple function and the contained loop, but still nothing, google/youtube seem to be no help either.

Here's the code:

AcronymList = []
DefinitionList = []
ListTuple = []


def openFile(filename, list):
    Text_File = open(filename, 'r ') #Open and store text file as list
    global TextFileLineCount
    TextFileLineCount = Text_File.readlines()#Count the lines in the file
    for line in TextFileLineCount:
        list.append(line.strip())  # Removes unneccessary garbage
    Text_File.close()
    #print(list)

def WordListToTuple():
    count = 0
    for count in range(len(TextFileLineCount)):
        ListTuple[count].append(tuple((AcronymList), (1)))


getWordListFileName = input("Enter the filename and extension for the word list:  ")
openFile(getWordListFileName, AcronymList)
WordListToTuple()

CodePudding user response:

just do ListTuple.append(tuple(AcronymList, 1))

CodePudding user response:

Guessing you want list_tuple to contain one tuple (item, 1) for every item in acronym_list, just tell Python:

    list_tuple = [ (item, 1) for item in acronym_list ]

CodePudding user response:

list = [] tup = (1,2,3,4,5) for i in tup: list.append(i)

  • Related