When I iterate over a dictionary like so:
dict2={ 'Joe':('Caucasian','Male', 35, 7.5), 'Kevin':('Black','Male', 55, 9.5), More tuples here like the one above } The data is bigger but it doesn't matter here.
What I am trying to accomplish is to create a new dictionary with the information from the tuples. Like so:
dict_i_want = { "Name": Joe, "Ethiniticy": "Caucasian", "Gender":"Male", "Voter_age": 35, "Score": 7.5 } Here is my code:
dict_i_want = {} for k,v in dict2.items(): dict_i_want["Name"] = k dict_i_want['Ethiniticy'] = v[0] dict_i_want['Gender'] = v[1] dict_i_want['Voter_age'] = v[2] dict_i_want['Score'] = v[3] But when I do
print(dict_i_want) {'Name': 'Kevin', 'Ethiniticy': 'Black', 'Gender': 'Male', 'Voter_age': 55, 'Score': 9.5} The result is just the last tuple that I have in mydict2. No all the tuples. What I am doing wrong if I have the loop?
PS: I don't want to use any modules or import anything here. No built-in function like zip() or something like that. I want to hard code the solution
https://stackoverflow.com/questions/66737798/python-creating-dictionary-from-list-and-tuple March 22, 2021 at 05:34AM
没有评论:
发表评论