Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 question on list in Python
#1
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
Quote
#2
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.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)