0

I have a tuple list of university names and I'm trying to get it to store each university name as a string in a list. I have tried the following code. Any thoughts on how to achieve this?

def convertTuple(tup):
    str = ''
    for item in tup:
        str = str + item
    return str

main():

school = convertTuple(schoolName)

print(school)




schoolName = [('Carthage',), ('Clarkson',), ('F&M',), ('GMU',), ('Georgia_Tech',), ('IIT',), ('Kenyon',), ('MIT',), ('McDaniel',), ('Michigan_State',), ('NIU',), ('Oklahoma_State',), ('Penn_State',), ('Princeton',), ('Purdue',), ('Radford',), ('Ramapo',), ('Randolph_Macon',), ('Rowan',), ('Tarleton',), ('TCU',), ('Texas_State',), ('Tulane',), ('UMASS_Lowell',), ('University_Of_Alabama',), ('UC_Berkeley',), ('UC_Riverside',), ('Maine',), ('UMBC',), ('UNO',), ('South_Alabama',), ('South_Carolina',), ('UT_Austin',), ('Toronto',), ('University_Of_Washington',), ('Ursinus',), ('Washington_State',), ('Wisconsin',)]
2
  • 1
    Is main(): supposed to be def main():? Where do you call it? The indentation is messed up. Commented Jun 13, 2022 at 22:35
  • 1
    side note: do not use str as a variable name. It shadows the built-in str class. Commented Jun 13, 2022 at 22:40

2 Answers 2

2

You're not accessing the elements in the tuples. The school name is item[0].

You're also concatenating the names into a string, not creating a list. Use a list comprehension to create a list.

def convertTuple(tuples: list) -> list:
    return [item[0] for item in tuples]
Sign up to request clarification or add additional context in comments.

7 Comments

is there a way in this approach to store each school name into a separate element in a list
return [item[0] for item in tuples]. But your code is concatenating them, not putting them into a list.
or [item for item, in tuples]
@S.B Yeah, I really dislike that syntax for 1-element tuples, it's too easy to miss the ,. item[0] is only 2 more characters and is much clearer.
right ! compelling...
|
0

you have to collect the string which is in the tuples

def convertTuple(tup):
    listOfSchoolName = []
    for item in tup:
        listOfSchoolName.append(item[0])

    return listOfSchoolName
schoolName = [('Carthage',), ('Clarkson',), ('F&M',), ('GMU',), ('Georgia_Tech',), ('IIT',), ('Kenyon',), ('MIT',), ('McDaniel',), ('Michigan_State',), ('NIU',), ('Oklahoma_State',), ('Penn_State',), ('Princeton',), ('Purdue',), ('Radford',), ('Ramapo',), ('Randolph_Macon',), ('Rowan',), ('Tarleton',), ('TCU',), ('Texas_State',), ('Tulane',), ('UMASS_Lowell',), ('University_Of_Alabama',), ('UC_Berkeley',), ('UC_Riverside',), ('Maine',), ('UMBC',), ('UNO',), ('South_Alabama',), ('South_Carolina',), ('UT_Austin',), ('Toronto',), ('University_Of_Washington',), ('Ursinus',), ('Washington_State',), ('Wisconsin',)]

school = convertTuple(schoolName)

print(school)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.