Looping through list of dictionaries.. | Sololearn: Learn to code for FREE!


Looping through list of dictionaries..

"""" I want to print sthg like : sagar249: first name: Sagar last name: Thapa city: Berlin sujan10: ..... But what i get is the whole dictionary instead of just username 'sagar249' Help me !!! """ usernames = [] sagar249= { 'first name': 'sagar', 'last name': 'thapa', 'city': 'berlin', } sujan10= { 'first name': 'sujan', 'last name': 'schönes', 'city': 'stuttghart', } shrawan07= { 'first name': 'shrawan', 'last name': 'singh', 'city': 'kathmandu', } usernames.append(sagar249) usernames.append(shrawan07) usernames.append(sujan10) for username in usernames: print(str(username) + ":\n") for key, value in username.items(): print("\t" + key.title()+ ": " + value.title()) print("\n\n")

8/8/2021 3:52:40 PM


8 Answers

New Answer


Here's a code that might help: usernames = [('sagar249', sagar249), ('sujan10', sujan10), ('shrawan07', shrawan07)] for x, y in usernames: print(x + ":") for k in y: print(f" {k}: {y[k]}") # Hope this helps


You should tag the relevant language name.


Sacar use those tags to add the language name that is relevant with the code you have in description.


Calvin Thomas f-strings are new for me, anyways it's helpful ! Thank you!!


Sacar f-strings are cooler than the format method. You may refer sites like w3schools and all if you want to learn more. Happy coding!


Abhishek Kohli Please don't spam.


Abhay sorry can you be more specific ?


Hi The world Play games How to play