I am new to python and programming in general. I looked at a lot of questions about dictionaries, lists, map, lambda, list comprehensions, etc., but I cannot seem to put a specific function together to build the dictionary that I want.
I can describe the dictionary that I want to produce as follows:
#goal: make a dictionary that describes a form
FORMNAME = 'aspnetForm'
nicks = ['username','password','submit'] #nicknames I want to use
names = [FIELD_USERNAME, FIELD_PWD, BUTTON_LOGIN] #field names
values = ['myusername','mypass',''] #field values
types = ['text', 'password', 'submit'] #control types
INPUTS = [{nicks[0]:{'name':names[0],'value':values[0],'type':types[0]}},
{nicks[1]:{'name':names[1],'value':values[1],'type':types[1]}},
{nicks[2]:{'name':names[2],'value':values[2],'type':types[2]}}]
LOGINFORM = {'name':FORMNAME, 'input':INPUTS} #this is the final dictionary I want to build
Please, impress me with the simplicity and elegance of the best python method to build LOGINFORM so that I may learn!
I want to use this dictionary to populate a form in mechanize. It’s the only form I need to populate. I can populate this form the “long” way, but I wanted to learn how to populate it in a systematic way with a dictionary.
My main purpose here is to learn how to populate dictionaries. But I thank you in advance for not suggesting a better way until I learn how to answer my own question.
You can make use of
zip:I’m avoiding the use of
typeas variable name, because it’s a builtin function/type, but actually you can use it here just fine.