PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 15, 2022

[FIXED] How to concatenate different string after each dictionary entry

 September 15, 2022     dictionary, printing, python, string     No comments   

Issue

{0: {'Name': 'Tom', 'Age': 20},
 1: {'Name': 'Joseph', 'Age': 21},
 2: {'Name': 'Krish', 'Age': 19},
 3: {'Name': 'John', 'Age': 18}}

I want to print each element of the dictionary followed by a string concatenated as shown below. it is easy to print a same string concatenated after each element but i dont get to understand how to have a different string concatenated after each dictionary element.

A sample output is as shown below:

0 : {'Name': 'Tom', 'Age': 20} is more similar to
1 : {'Name': 'Joseph', 'Age': 21} than to
2 : {'Name': 'Krish', 'Age': 19} than to
3 : {'Name': 'John', 'Age': 18}}

Solution

Do you mean something like this:

my_dict = {...}
outputs = ['is more similar to', 'than to', 'than to', '']
i = 0
for key, val in my_dict.items():
    print(str(key) + ':', val, outputs[i])
    i += 1


Answered By - The Thonnu
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing