My list of lists looks like this:
list_of_lists = [['England', '90.0%'], ['Scotland', '10.0%']]
I would like to have this output:
England, 90.0%
Scotland, 10.0%
I have tried unpacking the list of lists and printing using the following:
a,b = list_of_lists
print(a,'\n',b)
but I would like to print them dynamically based on the length of the list_of_lists. So if my list_of_lists is len(x) then I want to print(x[0],'\n\',...,x[i])
CodePudding user response:
You can join the elements per row using ', ' as separator, then join the rows with a newline and print:
list_of_lists = [['England', '90.0%'], ['Scotland', '10.0%']]
print('\n'.join(map(', '.join, list_of_lists)))
output:
England, 90.0%
Scotland, 10.0%
CodePudding user response:
Simple for loop?
for row in list_of_lists:
print(', '.join(row))
CodePudding user response:
You could use unpacking of a generator that formats each entry and use a new line as the print separator:
print(*(f'{c}, {p}' for c,p in list_of_lists),sep='\n')
This supports different data types and gives you full control over the order, spacing, alignment and delimiters
If your data is only made up of strings that you want to separate with commas, you can use join instead of a format string:
print(*map(', '.join,list_of_lists),sep='\n') # only works with string data
