Basic operations:
a = [] # creates an empty list
a = [1]*5 # creates [1, 1, 1, 1, 1]
L = [2]*3 # creates [2, 2, 2]
a.append( 12) # appends an element
a.extend( L) # appends a list to a list [1, 1, 1, 1, 1, 2, 2, 2]
a.sort() # sorts a list
a.reverse() # reverses a list
del a[0] # deletes an element
del a # deletes the list
len( a) # the length
a = ['spam', 'eggs', 100, 1234]
a[0] # 'spam', first element
a[-1] # 1234, last element
a[0:2] = [1, 12] # [1, 12, 100, 1234]
a[0:2] = [] # [100, 1234]
a[1:1] = ['bletch', 'xyzzy'] # [1, 'bletch', 'xyzzy', 1234]
b = a[2:] # ['xyzzy', 1234]
b = a[:] # copies a list, see below
Manipulations:
q = [2, 3]
p = [1, q, 4]
p[1] # [2, 3]
p[1][0] # 2
p[1].append('xtra') # [1, [2, 3, 'xtra'], 4]
q # [2, 3, 'xtra']
r = [4, 5]
s = q + r # [2, 3, 4, 5] list concatenation
s.insert( 1, 123) # [2, 123, 3, 4, 5]
s.pop() # [2, 123, 3, 4]
s.pop(0) # [123, 3, 4]
Printing:
freshfruit = [' banana', ' loganberry ', 'passion fruit ']
print( [weapon.strip() for weapon in freshfruit])
Searching:
if elm in list:
print( elm, "is in the list")