Search code examples
stringlistsplitreversenonetype

Why this code is giving output as none. I was trying to get the reversed output


Why the following code is giving me output as none. Basically, I attempt to get the output as Lastname Firstname. So I used split() so that it will return me the list and then I tried to reverse the list using reverse(). Though I'm getting output as none.

CODE:

fn="FirstName LastName".split(" ")
a=fn.reverse()
print(a)

Solution

  • list.reverse() changes list "in-place", so your a variable is None.

    Try:

    fn = "FirstName LastName".split(" ")
    fn.reverse()
    print(" ".join(fn))
    

    Prints:

    LastName FirstName