Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Differentiate and replace single for multiple string list elements

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading