I want to return a dictionary of elements by assigning each value of a list to “like” but I keep coming up with an error. This is my code.
def f([a,b,c]):
d={a:'like'}
for x in [a,b,c]:
d[x]='like'
return d
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
My guess is that this is what you want to do:
Example:
In Python 2.7 or above, the function can also be written as
Regarding your original code: There is no such syntax as
def f([a, b, c]). I can only guess what this is supposed to mean, there are no square brackets in a parameter list. If you want to pass in a list, use a single parameter.In the updated version of the code the other errors were removed. The initialization of
dis still strange since it would be enough to use an empty dictionary: