0

I am new and this is possibly a newbie question. I am using Python. So here's the code:

def student_list (names):
    unique = [ ]
    for name in names:
        if name not in unique:
            unique.append(name)
    return unique
    print(name)        

def listToString(names):
        str1 = " "
        return (str1.join(names))
                
names = ["Adam", "Ben", "Aaron", "Clyde", "Alex", "Billy", "Chris", "Adam", "Clyde"]
names.sort()
    
print(listToString(student_list(names)))

The code will result in:

Aaron Adam Alex Ben Billy Chris Clyde

But what I want to achieve is:

Aaron
Adam
Alex
Ben
Billy
Chris
Clyde

Also a side question, is it possible to have two for-loops within a nested function?

Thank you.

S7YF4WX
  • 1
  • 1

1 Answers1

0

Just write the list to string function like this

def listToString(names):
        str1 = "\n"
        return (str1.join(names))

Notice the "\n" as the join character instead of " ".

To answer the second one, yes there can be any number of nested loops inside a function or a nested function.

leangaurav
  • 393
  • 2
  • 11