Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add number, then tuple to list as a tuple, but it drops outer tuple [duplicate]

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?

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

l2 = [(1.0, (2.0,3.0)),
      (4.0, (5.0,6.0))]
print(l2)

l1 comes out as [1.0, (2.0, 3.0), 4.0, (5.0, 6.0)]

l2 comes out as [(1.0, (2.0, 3.0)), (4.0, (5.0, 6.0))]

like image 803
user2868623 Avatar asked Jun 21 '16 02:06

user2868623


1 Answers

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)
like image 124
dmitryro Avatar answered Sep 27 '22 16:09

dmitryro