Python Forum
question on list in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: question on list in Python (/thread-27984.html)



question on list in Python - spalisetty06 - Jun-30-2020

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


RE: question on list in Python - pyzyx3qwerty - Jun-30-2020

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.