A Hundred Days of Code, Day 008 - Python Basics, Lists, Tuples, Dictionaries, Sets and Done!
This was an amazing run! Learnt lots. Done with the basics course. Woohoo! Notes With lists Not pythonic to mix types can iterate, slice and dice, search, get lengths, transmute them (for e.g. reverse items in them) just like strings can have lists of lists! lists are mutable, unlike strings. can change items/elements of a list If i create a list from another list, the new list items reference the old list items if i want to copy elements from a list rather than reference them, I’ll go newlist = oldlist[:] which will copy the elements from the old list into the new one, rather than create references to the old ones Realise that there is a difference between changing the values of a variable and between changing elements of a list. When I do x = 2 and y = x and then change x to be x = 3, remember that the value of y does not become 3. y continues to point to the original location in memory which holds 2. What I did was to break x’s connection to that value and assign to another value in another location On the other hand, when I do x = ['jane', 'jill', 'mary'] and then create a copy with y = x and then change x[0]='edith', what I do in this case is change the actual element. The references to that object in momory stay unchanged. Which is why y[0], which holds references to the same object, will also reflect the change to edith. Finally if I go, x = ['athena', 'diana', 'aphrodite'], now we’re back to the assignment. x will point to this new list while y points to the old one. This distinction between changes in assignment vs changes to content is very important, when dealing with mutable/changeable data types like lists Append something to a list with .append - list.append(something, something else) Heuristic, when a method on a mutable data type, changes data, in returns None So I should just go mylist.append('blah') and stuff will be done If go mylist = mylist.append('blah') (which I am very apt to do) the method will change the list, then return None and that None will get assigned to mylist, In effect, erasing the list and causing unnecessary heartache. So, be careful, very careful. Not careless, like Bond, James Bond. += or .extend takes an iterable on the right. picks each element. and then assigns them singly to the list on the left. Append can take only one item. If I want to add xyz as separate entries to mylist that has ['a','b','c'], append will just take it as a big lump and make ['a','b','c','xyz'] Instead I do a += mylist += 'xyz' or mylist.extend('xyz')will give me ['a','b','c','x','y','z'], which is what I wanted in the first place it will only work if there are iterables on the right. (strings, lists, dictionaries etc.) the insert method will let you insert something at a location in the list. Give it what you want, tell it where you want, and boom! mylist.insert('d', 3) will put d after abc, giving me ['a','b','c','d','x','y','z'] I can replace things in a list by giving it a location and telling it what to put in there. I could do mylist[0]='artemis' and the list will change to ['artemis','b','c','d','x','y','z'] I could also give it a slice along with a list of what to put in there. Doing mylist[1:2]=['josephine','lisbeth'] will now give me ['artemis','josephine','lisbeth','d','x','y','z'] if you add additional elements, than what you replace, Python will insert them, growing the list. Removing things from a list? the .pop method will pop off the last item in a list. If i give it a location, it will remove the item at that index location. mylist.pop(3) will remove d from mylist giving me ['artemis','josephine','lisbeth','x','y','z'] When I know what I want to remove, I just use the remove method instead of pop. mylist.remove('josephine') will remove poor Josephine leaving us with ['artemis','lisbeth','x','y','z'] If I’m not sure, I could check for the presence of an item before I remove it. Should prevent a program from barfing on me if that element isn’t there if 'q' in mylist: mylist.remove('q') also the remove method only removes the first instance of something. Iterating and counting over lists? the for loop! I can use the enumerate function if I want numbered items out I can iterate over a slice. The range function is very list like in its behaviour. I can sort lists. same caveat as above. sort will change the list in place and return None. Don’t go assigning mylist = mylist.sort(). It won’t work A .sort(reverse=True) will sort in reverse sorting between mixed types in a list doesn’t work in Python 3 anymore. The types in a list, ought to be the same. Sorting list of lists works by comparing indices If I don’t want to change my original list, I can use the sorted function on the list. It works on most iterables, not just lists, while the list method is restricted to lists It returns a new sorted list, and keeps the original list intact. I can do sorted('something', reverse=True) will return a reversed list. I can str a list and list a string ...