-1

I want to round some numbers inside of a list by using f-string, but it returns

unsupported format string passed to list.__format__

This is my code

IS_gradebooks = [['ISOM2020', [59, 100, 80, 55, 95, 87, 95, 98, 74, 69, 92, 94, 75, 97, 43, 57]],
                 ['ISOM3400', [98, 73, 45, 88, 72, 94, 82, 100, 89, 52]], 
                 ['ISOM3600', [24, 44, 100, 81, 91, 93, 87, 72, 55]],
                 ['ISOM4200', [90, 56, 78, 67, 90, 93]]
                ]
bruh=IS_gradebooks
for i in range(len(bruh)):
  a=sum(bruh[i][1])/len(bruh[i][1]) 
  bruh[i][1]=a
print(f"{bruh:.2f}")
Gartz man
  • 3
  • 1
  • 1
    This is a good example of where you should show us exactly what you want our output to look like – Chris Jan 27 '23 at 00:22
  • Sorry, ouput i want is [['ISOM2020', '79.38'], ['ISOM3400', '79.30'], ['ISOM3600', '71.89'], ['ISOM4200', '79.00']] – Gartz man Jan 27 '23 at 00:28
  • `bruh` is a dictionary. `.2f` format can only be used when formatting a float. The format specification is not applied recursively to the numbers inside the list. – Barmar Jan 27 '23 at 00:28
  • What's the point of doing `bruh=IS_gradebooks`? I hope you know that doesn't make a copy of the dictionary, you're still modifying `IS_gradebooks`. – Barmar Jan 27 '23 at 00:30
  • What you are doing is trying to round off a list usinf f string which is why you are getting error Try this instead :- `bruh = [[x, f"{y: .2f}"] for x, y in bruh]` – Nehal Birla Jan 27 '23 at 00:36
  • `[[x[0], f'{sum(x[1])/len(x[1]):.2f}'] for x in IS_gradebooks]` – Chris Jan 27 '23 at 00:38

1 Answers1

0

You can change the variable bruh[i][1] to "{0:.2f}".format(a) and print that variable inside the loop:

for i in range(len(bruh)):
  a=sum(bruh[i][1])/len(bruh[i][1]) 
  bruh[i][1]="{0:.2f}".format(a)
  print(bruh[i])
amkawai
  • 353
  • 3
  • 10