Initializing list in Python as blank is the most memory efficient way of doing it. Logically this won't make sense, but the program below will prove.
Output:
declare a with one element and get size of x
[]
64
declare a with one element and get size of a
['a']
72
declare b with two elements and get size of b
['a', 'b']
80
declare c with two elements and get size of c
['a', 'b', 'c']
88
append one element and see size of
['a']
96
['a', 'b']
104
['a', 'b', 'c']
112
append another element-list and see size of
['a', ['b', 'c']]
96
['a', 'b', ['c', 'd']]
104
['a', 'b', 'c', ['e', 'f']]
112
append another grouo of elements and see size of
['a', ['b', 'c'], 'd', 'e']
96
['a', 'b', ['c', 'd'], 'e', 'f']
104
['a', 'b', 'c', ['e', 'f'], 'g', 'h']
112
from sys import getsizeof
print('declare a with one element and get size of x')
x = []
print(x)
print(getsizeof(x))
print('declare a with one element and get size of a')
a = ['a']
print(a)
print(getsizeof(a))
print('declare b with two elements and get size of b')
b = ['a','b']
print(b)
print(getsizeof(b))
print('declare c with two elements and get size of c')
c = ['a', 'b', 'c']
print(c)
print(getsizeof(c))
print('append one element and see size of')
x.append('a')
print(x)
print(getsizeof(x))
a.append('b')
print(a)
print(getsizeof(a))
b.append('c')
print(b)
print(getsizeof(b))
print('append another element-list and see size of')
x.append(['b', 'c'])
print(x)
print(getsizeof(x))
a.append(['c', 'd'])
print(a)
print(getsizeof(a))
b.append(['e', 'f'])
print(b)
print(getsizeof(b))
print('append another grouo of elements and see size of')
x.extend(['d', 'e'])
print(x)
print(getsizeof(x))
a.extend(['e', 'f'])
print(a)
print(getsizeof(a))
b.extend(['g', 'h'])
print(b)
print(getsizeof(b))
declare a with one element and get size of x
[]
64
declare a with one element and get size of a
['a']
72
declare b with two elements and get size of b
['a', 'b']
80
declare c with two elements and get size of c
['a', 'b', 'c']
88
append one element and see size of
['a']
96
['a', 'b']
104
['a', 'b', 'c']
112
append another element-list and see size of
['a', ['b', 'c']]
96
['a', 'b', ['c', 'd']]
104
['a', 'b', 'c', ['e', 'f']]
112
append another grouo of elements and see size of
['a', ['b', 'c'], 'd', 'e']
96
['a', 'b', ['c', 'd'], 'e', 'f']
104
['a', 'b', 'c', ['e', 'f'], 'g', 'h']
112
Comments
Post a Comment