+ 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")

8th Aug 2021, 3:52 PM
Sacar
Sacar - avatar
8 Antworten
+ 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
8th Aug 2021, 4:08 PM
Calvin Thomas
Calvin Thomas - avatar
+ 4
You should tag the relevant language name.
8th Aug 2021, 3:58 PM
Abhay
Abhay - avatar
+ 3
Sacar use those tags to add the language name that is relevant with the code you have in description.
8th Aug 2021, 4:07 PM
Abhay
Abhay - avatar
+ 1
Calvin Thomas f-strings are new for me, anyways it's helpful ! Thank you!!
8th Aug 2021, 4:13 PM
Sacar
Sacar - avatar
+ 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!
8th Aug 2021, 4:21 PM
Calvin Thomas
Calvin Thomas - avatar
0
Abhishek Kohli Please don't spam.
10th Aug 2021, 8:35 AM
Calvin Thomas
Calvin Thomas - avatar
- 2
Abhay sorry can you be more specific ?
8th Aug 2021, 4:02 PM
Sacar
Sacar - avatar
- 2
Hi The world Play games How to play
10th Aug 2021, 7:21 AM
Abhishek Kohli