Nested Dictionary Comprehension Useless?
Yesterday I was stumped by a question about nested dictionary comprehension. I have to admit that I don't use dictionary comprehension often, especially nested ones. My preferred way is dict ( zip (list1, list2)) Anyway, I felt the need to go over dictionary comprehension, and I found something really interesting about nested dictionary comprehension -- it probably doesn't have any practical applications. >>> def my_list_comp (): ... a = range ( 3 ) ... b = range ( 3 , 6 ) ... return [(x, y) for x in a for y in b] ... >>> def my_dict_comp (): ... a = range ( 3 ) ... b = range ( 3 , 6 ) ... return {x: y for x in a for y in b} ... >>> my_list_comp() [( 0 , 3 ), ( 0 , 4 ), ( 0 , 5 ), ( 1 , 3 ), ( 1 , 4 ), ( 1 , 5 ), ( 2 , 3 ), ( 2 , 4 ), ( 2 , 5 )] >>> my_dict_comp() { 0 : 5 , 1 : 5 , 2 : 5 } >>> The behavior of list comprehension is straightforward, but what ab...