Python List functions do quite a job for simple tasks. Though I would recommend Numpy arrays if the application is required to be efficient. Go through the functions available to work on Python Lists
from sys import getsizeof
print("create list")
a = ['a','b']
print(a)
print("space allocated")
print(getsizeof(a))
print(getsizeof(a[0]))
print(getsizeof(a[1]))
print("add element to list")
a.append('c')
print(a)
print("add element at a specific location")
a.insert(1, 'd')
print(a)
print("add list at the end of a list")
print(a+['e', 'f'])
a.extend(['e','f'])
print(a)
print("remove an element")
(a.remove('e')) if 'e' in a else (print("not in list"))
print(a)
print("index of an element using index function")
(print(a.index('e'))) if 'e' in a else (print("not in list"))
try:
print(a.index('e'))
except ValueError:
print("Value Error")
print("find length of a")
print(len(a))
print("assigning to other variable")
b = a
print("Now both a and b point to the same list.")
print("replicating a list")
import copy
c = copy.copy(a)
print(a)
print(c)
d = ['a', ('a', 'b'), ['a', 'b'], {'a', 'b'}]
e = copy.copy(d[1][1])
print(d)
print(e)
f = copy.deepcopy(d[1][1])
print(f)
print("sort using sort()")
print(a)
a.sort()
print(a)
print("reverse")
print(a)
a.reverse()
print(a)
print("pop last")
print(a)
a.pop()
print(a)
print("pop second element")
print(a)
a.pop(1)
print(a)
Comments
Post a Comment