Lists
my_list = list()
my_list = []
to String
my_list = ['a', 'b', 'c', 'd', 'e']
"".join(my_list)
'abcde'
Array to String
my_array = [1, 4, 9]
s = ''.join(map(str, my_array))
# to int
int(s)
#### Add O(1)
```python
# Adds to right end
my_list.append(item)
Pop O(1)
# Removes from right end
my_list.pop()
Pop(item) O(n)
# Removes item
my_list.pop(item)
Insert O(n)
# Inserts at given position after provided idx
my_list.insert(idx, item)
# At beginning
my_list.insert(0, item)
Delete O(n)
a = [-1, 1, 66.25, 333, 333, 1234.5]
del a[0]
a
del a[2:4]
a
del a[:]
a
Sorting
sorted_list = sorted(my_list) # No side effects
my_list.sort() # Modifies in place
Tricks
#List traversal
range(start, stop, hop)
range(n) # [0,1,...,n-1]
range(1,n) # [1,...,n-1]
range(1,n,2) # [1,3,5,...,n-1] if n is even, or [1,3,5,...,n-2] if n is odd
range(n,-1,-1) # [n,n-1,n-2,...,0]
range(len(arr)) # Provides indices of an array arr
range(len(arr)-1,-1,-1) # Provides indices of arr backwards
# List slicing
arr[w:s] # Wait w elements, start copy (:), stop before reaching index s
arr = [1,2,3,4]
arr[1:] = [2,3,4]
arr[:2] = [1,2]
#List manipulation
arr = [1,2,3]
[str(x) for x in arr] # Output: ['1','2','3']
map(lambda x: str(x), arr) # Output: ['1','2','3']
[str(x) for x in arr if x%2] # Output: ['1','3']
# List as queue
arr = [1,2,3]
arr.append(x) # queue.push(x)
arr.pop(0) #queue.pop()
arr[0] #queue.peek()
# List as stack
arr = [1,2,3]
arr.append(x) #stack.push(x)
arr.pop() # stack.pop()
arr[-1] # stack.peek()