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

后端 未结 2 911
耶瑟儿~
耶瑟儿~ 2020-12-21 02:49

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)

相关标签:
2条回答
  • 2020-12-21 03:35

    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.

    0 讨论(0)
  • 2020-12-21 03:40

    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
    
    0 讨论(0)
提交回复
热议问题