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

+1

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

Sacar

8 Answers

New Answer

+6

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

+4

You should tag the relevant language name.

+3

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

+1

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

+1

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!

0

Abhishek Kohli Please don't spam.

-2

Abhay sorry can you be more specific ?

-2

Hi The world Play games How to play