list('JUICE') and ['JUICE']

print(list('JUICE'))
#returns ['J', 'U', 'I', 'C', 'E']
print(['JUICE'])
#returns ['JUICE']

Why are these two codes returning different outputs??
I thought those should be returning the same output since those are making the value inside each list function and into list.

Hey @nayunsung3562491092, by the way, I formatted your code, check out this post for more details.

This happens because, in the first line, you are printing a list of each individual character in a string. This is what the list function does to a string, it converts each element of the string to a list element (excluding including spaces).

The second command just prints the word “JUICE” as a list element.

2 Likes

Not sure it would ignore the spaces… Python 3.7 certainly doesn’t…

>>> list("ICH MAG SAFT")
['I', 'C', 'H', ' ', 'M', 'A', 'G', ' ', 'S', 'A', 'F', 'T']

The behaviour is perhaps more obvious in languages which have a char type, as it’s then more obvious that the string is an array (list) of chars. :slight_smile:

3 Likes

Thank you so much for crystal clear explanation. Now I got it super clearly. :smiley:
So if I’m not mistaken with your explanation, on the first line list function will turn the string JUICE into a list with elements of J, U, I, C, E and the second list [‘JUICE’] is just taking the whole word as an element! :+1:

Thank you for the nicely described examples and explanations. That really helped me a lot in understanding the concept of list. Space element has been always tricky to me. :laughing:

1 Like