Add number, then tuple to list as a tuple, but it drops outer tuple?

DWQA QuestionsCategory: PythonAdd number, then tuple to list as a tuple, but it drops outer tuple?
Editor Staff asked 2 weeks ago

I am trying to add a tuple of a (number, (tuple)), but it drops the outer tuple.

How do I change the code so that l1 comes out looking like L2? It appears to drop the outer tuple and convert it to list elements? How do I stop that? Better yet, why is it happening?

2 Answers
Editor Staff answered 2 weeks ago

Use append:
l1 = []
t1 = (1.0, (2.0, 3.0))
l1.append((t1))
t2 = (4.0, (5.0, 6.0))
l1.append(t2)
print(l1)
l2 = [(1.0, (2.0, 3.0)),
(4.0, (5.0, 6.0))]
print(l2)

Editor Staff answered 2 weeks ago

Use append:

l1 = []
t1 = (1.0, (2.0, 3.0))
l1.append((t1))
t2 = (4.0, (5.0, 6.0))
l1.append(t2)
print(l1)
l2 = [(1.0, (2.0, 3.0)),
(4.0, (5.0, 6.0))]
print(l2)