python – append tuples to a list

python – append tuples to a list

result.extend(item)

You can convert a tuple to a list easily:

>>> t = (AAA, 1.11)
>>> list(t)
[AAAA, 1.11]

And then you can concatenate lists with extend:

>>> t = (AAA, 1.11)
>>> result = [something]
>>> result.extend(list(t))
[something, AAA, 1.11])

python – append tuples to a list

You can use the inbuilt list() function to convert a tuple to a list. So an easier version is:

l = [(AAAA, 1.11), (BBB, 2.22), (CCCC, 3.33)]
result = [list(t) for t in l]
print result

Output:

[[AAAA, 1.1100000000000001],
 [BBB, 2.2200000000000002],
 [CCCC, 3.3300000000000001]]

Leave a Reply

Your email address will not be published.