Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 691 Bytes

File metadata and controls

60 lines (43 loc) · 691 Bytes

List

Create a list

Default initialization

n = 5; # number of items
num = [0] * n
foo = [ i for i in range(5)]

With fixed initial values

n = 5; # number of items
num = [-1] * n

Sort in ascending order

num = [5, 4, 3, 2]
num.sort() # in place

sort in descending order

num = [5, 4, 3, 2]
num.sort(reverse=True)

Print the list

num = [1, 2, 3]
print(num)

Substring of a list

# Get elements form i to j
truncList = oldList[i:j+1]

Length of list

num = [1, 2, 3]
n = len(num)

Make a deep copy

oldList = [] 
newList = oldList[:]