I'm currently stuck with this output of a nested list when trying to append the list as the definition of my dictionary.
I know this is probably more code than I should include, but I figured it's helpful to show more than less.
class Account:
accountInfo = {} #ex. ID : 5FE19C (hexadecimal ID's)
def __init__(self):
choice = raw_input("Would you like to login or signup?\n")
if choice.lower() == "login":
self.login()
elif choice.lower() == "signup":
print "Great! Fill in the following."
self.signup()
else:
self.__init__()
def signup(self):
import random
accountID = '%010x' % random.randrange(16**10) # 10 digit hexadecimal ID generator
personalInfo = []
self.accountInfo[accountID] = []
firstName = raw_input("First Name: ")
lastName = raw_input("Last Name: ")
email = raw_input("E-Mail: ")
password = raw_input("Password: ")
birthdate = raw_input("DOB (DD/MM/YYYY): ")
alias = raw_input("Username/Alias: ")
personalInfo.append(firstName)
personalInfo.append(lastName)
personalInfo.append(email)
personalInfo.append(password)
personalInfo.append(birthdate)
personalInfo.append(alias)
for i in range(1):
self.accountInfo[accountID].append(personalInfo)
#creates an unwanted nested list, but the output is correct
print self.accountInfo
I don't understand why I'm getting the output of a nested list in my dictionary. The contents of the dictionary are correct, but it's just that unwanted and unnecessary nested list.
output:
>>> {'6de7bcf201': [['firstName', 'lastName', 'email', 'password', 'birthdate', 'alias']]}
personalInfo = [] # PersonalInfo is a list
# skipped
self.accountInfo[accountID].append(personalInfo) # add list to the list
This is similar to
main = []
p_info = []
main.append(p_info) # result would be main = [[]]
If you want to have just a dict inside list, change personalInfo to {} that would requare to change personalInfo.append to personalInfo[x] = y
personalInfo
is a list and self.accountInfo[accountID]
is another list.
With self.accountInfo[accountID].append(personalInfo)
you are injecting one inside the other.
You can do self.accountInfo[accountID] = personalInfo
And what's the point of for i in range(1)
? It's not looping at all!
I think this is pretty straight forward:
personalInfo
is a list. You append items to it and get something like [..,..,..]
. You initiate the value of self.accountInfo[accountID]
also as a list. Then you append your first list to the second list, giving you a list of lists.
Instead of self.accountInfo[accountID].append(personalInfo)
try self.accountInfo[accountID] = personalInfo
Welcome! A couple of things:
Imports should generally be at the top of a file. For your signup
function, the import random
should be at the top.
Your __init__
shouldn't be called again on a class. __init__
is considered the "constructor" of the class - as such it should generally be called once. Consider putting your raw_input
section in another function and call that twice.
Print is a function, not a statement (your last line)
To answer your question, you have a nested list because you are making two lists. You're first building up personalInfo
and then appending that item to the empty list you made for accountInfo
you can have a single-level list if you just set self.accountInfo[accountID] =
personalInfo`.
I can't test right now but:. Replace self.accountInfo[accountID] = []
with self.accountInfo[accountID] = personalInfo
. And delete for i in range(1): self.accountInfo[accountID].append(personalInfo)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.