TristenC
TristenC

Reputation: 1

How would I append a tuple to a list with a loop?

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()

Upvotes: 0

Views: 822

Answers (3)

greybeard
greybeard

Reputation: 2467

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 ]

Upvotes: 1

Devam Antahal
Devam Antahal

Reputation: 1

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

Upvotes: 0

Piradata
Piradata

Reputation: 1

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

Upvotes: 0

Related Questions