is it possible to do things like this:
def f():
d={}
d[1]='qwe'
d[2]='rty'
return d
a,b=f()[1:2]
not a,b=f()[1],f()[2]
and not
t=f()
a,b=t[1],t[2]
I wont avoid extra lines and call function only once.
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.
No; dictionary indexing doesn’t support slicing, since it usually doesn’t make any sense in dictionary keys.
You can roll it yourself, though, with something like
or (thanks @DSM)
These are slightly different in that if the result of
f()is missing1or2as keys, the first will default to assigningNoneand the second will raiseKeyError.map(f().__getitem__, ...)should be equivalent toitemgetter, though probably slightly slower.