21/01 - Lists

Run Settings
LanguagePython
Language Version
Run Command
#list - ordered sequence of objects of any type. Some examples: # li = [1,2,3,4,5] # li2 = ['a','b','c'] # lie3 = [1,2,'a', True] #lists are effectively a form of Array in python # lists are a data structure #Data Structure - way to org info/data into a folder, to use with diff pros and cons #the [] allows data to be contained # amazon_cart = ['notebooks', 'sunglasses'] # print(amazon_cart[0]) # as with strings/indexes, in a list each item is assigned a number, here 0 is 'notebooks' #list slicing - similar to string slicing where you'd have [0:2:1] for start/end/skip # amazon_cart = [ # 'notebooks', # 'sunglasses', # 'toys', # 'grapes'] # amazon_cart[0] = 'laptop'#this will now change item 0, notebooks, into laptop # #print(amazon_cart[0::2])# as with strings, this will start at 0, end at the end and skip every 2nd item # #lists are mutable/changeable # # print(amazon_cart[1:3]) # new_cart = amazon_cart[0:3] # new_cart[0] = 'gum' # print(new_cart) # print(amazon_cart) # here the original list is still printed, as list slicing creates a new copy #without list slicing e.g. [:], the original will be replaced instead of making a copy #e.g. new_cart = amazon_cart[:] will retain both carts when asked to print both #matrix - a way to describe multi-dimensional lists # matrixgoop = [ # [1,2,3], # [2,4,6], # [7,8,9] # ] # #the above provides a main array with 3 sub-arrays inside # print(matrixgoop[0][1]) # in a situation like this, the matrix will access the first section of the array [1,2,3], and then take the item in slot 1 from the first sub array, which is 2 #List Methods #basket = [1,2,3,4,5] #adding #new_list = basket.append(100) #changes the list in place, rather than creating a new one #basket.append(100)# in doing this individually, both lists will be = basket #basket.insert(4, 100) # adds 100 to the index 4 #basket.extend([100,101])# extends the list with new numbers provided #removing #new_list = basket #basket.pop() #removes whatever is at the end of the list, here it removes 5 #basket.pop(0) # removes the item at index 0, here it is 1 #basket.remove(4) # does what it says on the tin, give it a specific value to remove, rather than an index #print(basket) #new_list = basket.pop(4)#this will return the item that has been popped, here it will be index 4, number 5 #new_list = basket.clear()#as expected, this clears the whole basket #print(basket) #index #print(basket.index(2))#will give us index of the value, so here 2 is at index 1, will print 1 #print(basket.index(2,0,2))#this will give a start and stop point of 0,2 in the index #print(2 in basket)# the in function asks if value is in the list, giving true or false #print('i' in 'hi my name is ian')# checks if value is in the string #print(basket.count(2))# this will count how often the given value appears in the list #basket.sort()#will sort the basket in place #print(sorted(basket))# sorted produces a new list/array #print(basket)# the original basket will remain the same #basket.copy does the same as basket[:] #basket.reverse()#prints the basket in reverse order who'd have guessed, no sorting occurs #print(basket) #Common List Patterns # basket=['a','x','b','c','d','e','d'] # basket.sort() # basket.reverse() # print(basket[::-1])#this reverses the basket again, returning it to regular order. Slicing also creates a new list # print(basket) #print(list(range(100)))#range allows a list to be created with a range of values, e.g. here it will give 0-99 #sentence = ' ' #new_sentence = sentence.join(['hi','my','name','is','jojo'])# joins the contents with the str of sentence, such as a space #new_sentence = ' '.join(['hi','my','name','is','jojo'])# same situation without need for another variable #print(sentence) #print(new_sentence) #List Unpacking # a,b,c, *other, d = [1,2,3,4,5,6,7,8,9] # print(a) # print(b) # print(c) # print(other) # print(d) #None - special data type to represent absence of value, similar to Null in other lang #weapons = None #print(weapons)
Editor Settings
Theme
Key bindings
Full width
Lines