I'm working on a Python script where I need to format three different types of lists into a string that will be used as the body of an email. The lists are as follows:
A list of strings:
a = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
A list of dictionaries, each containing a single key-value pair:
b = [{'IP': '1.1.1.1'}, {'IP': '2.2.2.2'}, {'IP': '3.3.3.3'}]
A list of dictionaries with multiple key-value pairs:
c = [{'ID': 'dasfsdfs', 'IP': '1.1.1.1', 'Name': 'me'}, {'ID': 'qwrfgvzx', 'IP': '2.2.2.2', 'Name': 'you'}]
I want to format these lists so that each element (or key-value pair) appears on a separate line in the email body. Here's the structure I'm aiming for:
a_var -->
1.1.1.1
2.2.2.2
3.3.3.3
b_var -->
IP: 1.1.1.1
IP: 2.2.2.2
IP: 3.3.3.3
c_var -->
ID: dasfsdfs
IP: 1.1.1.1
Name: me
ID: qwrfgvzx
IP: 2.2.2.2
Name: you
I'm attempting to use inline for loops within an f-string to create this formatted string, but I'm not sure how to correctly apply this for each list, especially for the list of dictionaries with multiple key-value pairs.
my_email = f'''
a_var -->\n{inline for loop for {a}}
b_var -->\n{inline for loop for {b}}
c_var -->\ninline for loop for {c}}
'''
Can anyone suggest the correct way to achieve this formatting in Python?
Assuming that you want output like this:
For List
a:a" would contain '1.1.1.1', '2.2.2.2', etc.\n- basically it's like you are manually pressing enter each time.For List b:
keykey, you then write it as "IP" followed by key, again you're at the end joining it with new line char.For List c:
keyvalues (ID, IP, Name).To give you an idea: