Debugging Python code -


after few days still cannot figure out how make work on python (not experienced programmer). here code pasted , @ bottom, expected result:

color_list = ["red", "blue", "orange", "green"] secuence = ["color", "price"] car_list = []   def create_list(color_list, secuence, car_list):     num in range(len(color_list)):         car_list.append = dict.fromkeys(secuence)         car_list[%s]['color'] = color_list[%s] %num         return car_list 

this result want achieve:

>> car_list [{'color': 'red', 'price': none},{'color': 'blue', 'price': none},{'color': 'orange', 'price': none},{'color': 'green', 'price': none}] 

you can in 1 line particular scenario, assuming 'color' , 'price' constants.

car_list = [{'color': color, 'price': none} color in color_list] 

see list comprehension - it's powerful tool.


if had list of prices, price_list, similar:

car_list = [{'color': color, 'price': price} color, price in zip(color_list, price_list)] 

see built in function zip().


if strings 'color' , 'price' unknown @ time of execution (which think unlikely), similar following

car_list = [{secuence[0]: color, secuence[1]: price} color, price in zip(color_list, price_list)] 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -