Require each list element to be a single string. Second list element contains multiple strings:
suburbs = ['Wamuomata'],['Wan', 'omata'],['Eastboume']
Required to be:
suburbs = ['Wamuomata'],['Wan omata'],['Eastboume']
Code:
suburbs = ['Wamuomata'],['Wan', 'omata'],['Eastboume']
for x in range(len(suburbs)):
elementNumber = len(suburbs[x])
if elementNumber > 1:
for y in range(elementNumber):
print(suburbs[x][y])
print(suburbs)
Output:
Wan
omata
(['Wamuomata'], ['Wan', 'omata'], ['Eastboume'])
The output from print(suburbs[x][y]) references the list strings of element two correctly. However don’t know how to replace.
>Solution :
You can use str.join on each list to turn it into a single string:
>>> suburbs = ['Wamuomata'],['Wan', 'omata'],['Eastboume']
>>> tuple([' '.join(s)] for s in suburbs)
(['Wamuomata'], ['Wan omata'], ['Eastboume'])
Note that if you want a single list of strings, as opposed to a tuple of lists of a single string each, it’s simpler:
>>> [' '.join(s) for s in suburbs]
['Wamuomata', 'Wan omata', 'Eastboume']