1

I cannot seem to understand why my code is displaying this error.

IndexError: tuple index out of range

Code:

l = ['Simpson', ',', 'Bartholomew', 'Homer', 'G400', 'Year', '2']
x = '{}'* len(l)
print(x)
x.format(l)
print(x)
wim
  • 338,267
  • 99
  • 616
  • 750
Aresouman
  • 19
  • 1
  • 4

2 Answers2

1

Maybe you were looking for an unpacking:

>>> x.format(*l)
'Simpson,BartholomewHomerG400Year2'
wim
  • 338,267
  • 99
  • 616
  • 750
0

You are passing in just one argument, the list l, while your format string expects there to be 7 arguments.

If you wanted each element in l to be formatted, then use the *arg call syntax:

x.format(*l)

You want to print the return value, though:

result = x.format(*l)
print(result)

Demo:

>>> print(x.format(*l))
Simpson,BartholomewHomerG400Year2
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343