def items(dic):
li = []
for item in dic:
it = (item, dic[item])
li.append(it)
return li
How I can convert this into list comprehension?
CodePudding user response:
def items(dic):
return [(item, dic[item]) for item in dic]
CodePudding user response:
Like this:
li = [(item, dic[item]) for item in dic]
CodePudding user response:
The direct translation of your code would be [(item, dic[item]) for item in dic]
, however in this case no list comprehension is required assuming that dic
is a dict
object - you could use list(dic.items())
.