0

How can I append a tuple to a list? I am trying following:

>>> a = []
>>> a.append(tuple((str("{0}".format(5000)))))
>>> a
[('5', '0', '0', '0')]

Expected output is:

[('5000')]

Can someone help me fixing this issue?

Sweety
  • 301
  • 1
  • 9

2 Answers2

5

To add a one-value tuple use the notation (value, ), also no need use the str, "".format() is a string

a.append(("{0}".format(5000),))

Regarding the format you use, just do

a.append((str(5000),))

If we decompose it

t = ("{0}".format(5000),)
a.append(t)
azro
  • 53,056
  • 7
  • 34
  • 70
2

This is because python does not allow you to create a single element tuple directly. You must use one comma for this.

For example:

my_tuple = (100) #It's integer
my_tuple = (100,) #It's tuple

Try this for your code:

a.append(tuple((str("{0}".format(5000)),)))