''' lists.py demonstrates some of the features of python lists ''' from collections import deque import functools ''' The following lines demonstrtae how a list works in Python and some common list functions ''' list = [] # creates an empty list print(list) list = [10, 14.5, "Spongebob"] #lists can contain multiple kinds of data print(list) list = [x**2 for x in range(0,19)] ''' A list can be populated using list comprehensions, where we generate a series of numbers uingg a loop, inside the list notation ''' print(list) list2 =[10,20,30,40] ; list1 = [] list1.append("Krabby Patty") # append adds the item to the end of the list print(list2) print(list2[3], list2.index(10)) # We can query for a value using the index and # query for the index using the value list2 = [x*3 for x in range(0,20)] #another list comprehension ''' The following lines show how to slide and slice on a list ''' print(list2[3:15]) # prints list elemets 3 through 15 print(list2[14:]) # element 14 until the end print(list2[:5]) # from the beginning until element 5 print(list2[:]) # all of it print(list[12:19:2]) # from element 2 until 19, step size 2 print(list2[-1]) #last element print(list2[-2:]) #last 2 elements print(list2[:-2]) #everything except the last 2 ''' lists can be used to implement stacks and queues ''' stack = [12, 42, -87, 0, 3.5] stack.append(91) #push by appending to list print(stack) print(stack.pop()) #the pop function is available for all lists print(stack) ''' implementing a queue using traditional list functions is inefficient. So, we use a deque instead. ''' queue = deque([12,19, 8, 15, 25]) queue.append(150) # append to add to queue print(queue) print(queue.popleft()) # popleft to remove from queue print(queue) ''' The list structure comes with several other functions like sort and reverse''' list3 = [5,2,-10,19,34, 6, 99,0] list3.sort() list3.reverse() print(list3)