Python Forum

Full Version: question on list in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
Can you please explain to me in simple lines, the difference between "a = list("apple)" and "a = ["apple"]"?
Actually I was trying to append the character '_' to every character I find in the list.

WordToBeGuessed = ["Apple"]
Hidden = []
for EachCharacter in WordToBeGuessed:
    Hidden.append("_")
print(WordToBeGuessed)
print(Hidden)
gives me ['_']

while

WordToBeGuessed = list("Apple")
Hidden = []
for EachCharacter in WordToBeGuessed:
    Hidden.append("_")
print(WordToBeGuessed)
print(Hidden)
gives me ['_', '_', '_', '_', '_']

which is the desired answer but I just wanted to know the difference. Thank You
Basically, in the first code, the word 'Apple' is itself one character in the string, and so your output is ['_'] as there is only one character. However, in your second code, each letter in the word 'Apple' gets converted into an element in the list, and so, since there are 5 letters in 'Apple', your output is five underscores.