Nested list, printing information from a nested list. Use string format

Viewed 25

My_list = [ [2001, 9500, 6], [2002, 7500, 7], [2003, 8600, 3], ]

The list shows year, money, and how many people the money will be distributed to.

Use python to print out

2001: 1583.33 dollar 2002: 1071.43 dollar 2003: 2866.67 dollar
1 Answers

You can use str.format:

my_list = [
    [2001, 9500, 6],
    [2002, 7500, 7],
    [2003, 8600, 3],
]

for year, money, people in my_list:
    print("{}: {:.2f} dollar".format(year, money / people), end=" ")

print()

Prints:

2001: 1583.33 dollar 2002: 1071.43 dollar 2003: 2866.67 dollar 
Related