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
  • 302,178
  • 90
  • 548
  • 690
Aresouman
  • 19
  • 1
  • 4

2 Answers2

1

Maybe you were looking for an unpacking:

>>> x.format(*l)
'Simpson,BartholomewHomerG400Year2'
wim
  • 302,178
  • 90
  • 548
  • 690
  • Related: [*What does the Star operator mean?*](http://stackoverflow.com/q/2921847/416224) – kay Nov 30 '16 at 17:36
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
  • 963,270
  • 265
  • 3,804
  • 3,187