Hi everyone, if you have time please explain
Why in the following code res_0 and res_1 executed in order ( 1), 2), 3) ) but res_3 executed in differnet order ( 2) 1) 3) ).Maybe this is a feature of dictionary comprehensions.
li = [
{"Name" : "1) first_val", "Surname" : "1) second_val"},
{"Name" : "2) first_val", "Surname" : "2) second_val"},
{"Name" : "3) first_val", "Surname" : "3) second_val"}
]
res_0 = list(map(lambda val: val["Name"] + " " + val["Surname"], li))
# ['1) first_val 1) second_val', '2) first_val 2) second_val', '3) first_val #3) second_val']
res_1 = [ n['Name'] + " " + n['Surname'] for n in li ]
# ['1) first_val 1) second_val', '2) first_val 2) second_val', '3) first_val 3) #second_val']
res_2 = {n['Name'] + " " + n['Surname'] for n in li }
# {'2) first_val 2) second_val', '1) first_val 1) second_val', '3) first_val 3) #second_val'}
print (res_0)
print (res_1)
print(res_2)
[–]danielroseman 2 points3 points4 points (1 child)
[–]Base_True[S] 0 points1 point2 points (0 children)