List/Dict comprehension

List/Dict comprehension is a simple way to create lists and dicts with a bunch of values that we need. For really take advantage of this operator we must combine it with lambda operator, but first ,we will see simple examples.


[x * 2 for x in range(3)]
# output: [0, 2, 4] 
{chr(65+x) : x for x in range(3)}
# output: {'A': 0, 'C': 2, 'B': 1} 

Enter fullscreen mode Exit fullscreen mode

Now we will combine both operators:

serie = [x for x in range(20)]
f = lambda x: chr(x + 66) if x % 2 == 0 else chr(x + 65)
[f(x) for x in serie]
# output: ['B', 'B', 'D', 'D', 'F', 'F', 'H', 'H', 'J', 'J', 'L', 'L', 'N', 'N', 'P', 'P', 'R', 'R', 'T', 'T'] 

Enter fullscreen mode Exit fullscreen mode

原文链接:List/Dict comprehension

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容