Home > Software design >  How to concatenate different string after each dictionary entry
How to concatenate different string after each dictionary entry

Time:06-06

{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}}

CodePudding user response:

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
  • Related