▲ 1 r/learnpython
Hello everyone, I've been stuck on this for a while and would like to know how to get the ouput shown below. How can i access the strings inside the tuple? i tried before to loop 1 more level deep which looped over each individual string character (which is not what is needed here).
"""
countries = [[('Finland', 'Helsinki')], [('Sweden', 'Stockholm')], [('Norway', 'Oslo')]]
output i should be getting:
[['FINLAND','FIN', 'HELSINKI'], ['SWEDEN', 'SWE', 'STOCKHOLM'], ['NORWAY', 'NOR', 'OSLO']]
"""
countries = [[('Finland', 'Helsinki')], [('Sweden', 'Stockholm')], [('Norway', 'Oslo')]]
def flatten(collection):
return [list(subelement) for element in collection for subelement in element]
print(flatten(countries))
u/Fun-Pitch-6938 — 8 days ago