properties: ordered, iterable, mutable, can contain multiple data types
empty_list = []
empty_list = list()
simpsons = ['homer', 'marge', 'bart']
simpsons[0] # print element 0 ('homer')
len(simpsons) # returns the length (3)
simpsons.append('lisa') # append element to end
simpsons.extend(['itchy', 'scratchy']) # append multiple elements to end
simpsons.insert(0, 'maggie') # insert element at index 0 (shifts everything right)
simpsons.remove('bart') # searches for first instance and removes it
simpsons.pop(0) # removes element 0 and returns it
del simpsons[0] # removes element 0 (does not return it)
simpsons[0] = 'krusty' # replace element 0
neighbors = simpsons + ['ned','rod','todd']
simpsons.count('lisa') # counts the number of instances
simpsons.index('itchy') # returns index of first instance
weekdays = ['mon','tues','wed','thurs','fri']
weekdays[0] # element 0
weekdays[0:3] # elements 0, 1, 2
weekdays[:3] # elements 0, 1, 2
weekdays[3:] # elements 3, 4
weekdays[-1] # last element (element 4)
weekdays[::2] # every 2nd element (0, 2, 4)
weekdays[::-1] # backwards (4, 3, 2, 1, 0)
list(reversed(weekdays))
simpsons.sort()
simpsons.sort(reverse=True) # sort in reverse
simpsons.sort(key=len) # sort by a key
sorted(simpsons)
sorted(simpsons, reverse=True)
sorted(simpsons, key=len)
num = [10, 20, 40, 50]
from bisect import insort
insort(num, 30)
same_num = num
same_num[0] = 0 # modifies both 'num' and 'same_num'
new_num = num[:]
new_num = list(num)
id(num) == id(same_num) # returns True
id(num) == id(new_num) # returns False
num is same_num # returns True
num is new_num # returns False
num == same_num # returns True
num == new_num # returns True (their contents are equivalent)
regimentSize = (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211);
smallRegiments = list(filter((lambda x: x < 2500), regimentSize));
list_of_lists = [['Amy','Betty','Cathryn','Dana'],
['Elizabeth','Fay','Gora'],
['Heidi','Jane','Kayley']]
flattened_list = [i for row in list_of_lists for i in row]
items = [item for row in matrix
for item in row] # [1, 2, 3, 4]
first_names = ['Steve', 'Jane', 'Sara', 'Mary','Jack','Bob', 'Bily', 'Boni', 'Chris','Sori', 'Will', 'Won','Li']
def chunks(l, n):
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i+n]
list(chunks(first_names, 5))
[['Steve', 'Jane', 'Sara', 'Mary', 'Jack'],
['Bob', 'Bily', 'Boni', 'Chris', 'Sori'],
['Will', 'Won', 'Li']]