Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of casting lists to tuples in python and vice versa

what is the time complexity of converting a python list to tuple (and vice versa):

tuple([1,2,3,4,5,6,42])
list((10,9,8,7,6,5,4,3,1))

O(N) or O(1), i.e. does the list get copied or is something somewhere internally switched from writable to read-only?

Thanks a lot!

like image 780
SmCaterpillar Avatar asked Sep 10 '13 19:09

SmCaterpillar


2 Answers

It is an O(N) operation, tuple(list) simply copies the objects from the list to the tuple. SO, you can still modify the internal objects(if they are mutable) but you can't add new items to the tuple.

Copying a list takes O(N) time.

>>> tup = ([1, 2, 3],4,5 ,6)
>>> [id(x) for x in tup]
[167320364, 161878716, 161878704, 161878692]
>>> lis = list(tup)

Internal object still refer to the same objects

>>> [id(x) for x in lis]
[167320364, 161878716, 161878704, 161878692]

But outer containers are now different objects. So, modifying the outer objects won't affect others.

>>> tup is lis
False
>>> lis.append(10)
>>> lis, tup
([[1, 2, 3], 4, 5, 6, 10], ([1, 2, 3], 4, 5, 6)) #10 not added in tup

Modifying a mutable internal object will affect both containers:

>>> tup[0].append(100)
>>> tup[0], lis[0]
([1, 2, 3, 100], [1, 2, 3, 100])

Timing comparison suggest list copying and tuple creation take almost equal time, but as creating a new object with new properties has it's overhead so tuple creation is slightly expensive.

>>> lis = range(100)
>>> %timeit lis[:]
1000000 loops, best of 3: 1.22 us per loop
>>> %timeit tuple(lis)
1000000 loops, best of 3: 1.7 us per loop
>>> lis = range(10**5)
>>> %timeit lis[:]
100 loops, best of 3: 2.66 ms per loop
>>> %timeit tuple(lis)
100 loops, best of 3: 2.77 ms per loop
like image 169
Ashwini Chaudhary Avatar answered Oct 16 '22 07:10

Ashwini Chaudhary


As far as I understand, there's no bit to switch, as the list (mutable) object is completely different than the tuple (immutable) object. They have different methods etc.

One experiment you might do is this:

>>> a = [1,2,3,4,5]
>>> a = (1,2,3,4,5)
>>> a
(1, 2, 3, 4, 5)
>>> b = list(a)
>>> b
[1, 2, 3, 4, 5]
>>> b[2] = 'a'
>>> b
[1, 2, 'a', 4, 5]
>>> a
(1, 2, 3, 4, 5)

See, if they were referencing the exact place in the memory then a should have changed as well.

That's for my understanding.

like image 1
Ofir Israel Avatar answered Oct 16 '22 07:10

Ofir Israel