Skip to main content

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
  • I can split a string into a list, either by single characters or by a delimiter (a ; or a , or space)
    • split without an argument will cut out all white space
  • I can join, strings and lists and stuff, but

    • join works only on a string. it’s a string method
    • and that string generally is the glue, that will string what we give it, together
    • ' '.join(['Hail','Caesar']) will join the list using that blank space I gave it.
    • it works backwards, so that I can feed it any sequence in the end. a list, a tuple, a dictionary … all fair game
    • Remember, it won’t work on numbers. It’ll fail.
  • Tuples, are a kind of weird hybrid of strings and lists.

    • Like a string, they are immutable
      Like a list, they can contain anything at all.
    • crazyTuple = (1, 2, 4, 5)
    • you create them by enclosing with () brackets. but that is just for you to know you are creating a tuple. I can just do crazyTuple = 1 2 3 4 5 and that creates a tuple just as well.
    • I can do most everything with them just like strings and lists, slice, search, iterate blah blah
      • Typically used to create structures. like bookdetails = ('title', 'author', 'pubdate', 'price'). you know what to expect, once you see definition. Once we figure that [0] is the title, it normall will be the same for the other books too.
      • Behind the scenes in Python, function arguments are passed as tuples.
      • Note: If elements in my tuple are mutable (like a list), I can change their elements*, like appending or deleting items in the list. The tuple however is immutable. I cannot delete/modify the list item as a whole.
      • use extend if you want to append to a list in a tuple
      • Since a tuple is immutable, it doesn’t have a sort method. I can however sort by using the sorted function which will return a sorted list of the elements of the tuple, whill leaving the original tuple untouched.
  • Unpacking stuff

    • If my variables on the left, match up to the number of items in the structure on the right, x, y, z = (10, 20, 30) then they get assigned to each. x is assigned 10, y,20 and z,30.
  • Dictionaries

    • Hash tables, hash maps in other languages
    • While in a list, I have values of my choice, the index is always numeric. so item 0 is a cookie, item 1 is cookie-dough and so on, with a dictionary, I get to define the indexes too! I can now say endresult is a cookie, ingredient is cookie-dough and on and on
    • while if you want to append to a list, you use an append method, if you want to add keys (user defined indexes), you just assign it to a dictionary with its corresponding value. So I can just add cookie_recipe[sugar]=100 to my cookie recipe dictionary above. if the key is already in there, it just gets updated with the new value. if not, the key and the value get added to the dictionary.
    • Keys are unique. Values can repeat. But keys can exist only once in a dictionary
    • I can use most immutable data types as keys. strings, integers, tuples that don’t contain mutable data like lists etc.
    • I can use in to look for keys in a dictionary. 'sugar' in cookie_recipe
    • The get method will look for a key for you. If it exists it will return the key, or if there isn’t, it returns None. I could even customise the none with a message. If i do d.get('somekey', 'No such key'), it will print “No such key” if the key isn’t found.
    • The setdefault method will set a key in the dictionary along with the value, if it does not already exist in the dictionary. If it exists already, it will return the existing value.
  • If I want to convert strings to numbers and want to do a sanity check (I would not want to convert text after all, Hello is not a number :P) then I can use the isdigit method on the string. It’s a string method to use on strings.

  • Sets, can be thought of as dictionaries without any values.

    • Each value in a set can occur only once.
    • create them by going myset = set() and then I can willy-nilly add values with myset.add(10) or myset.add(32). If I try adding same value twice, no dice. It is stored only once. Every value is unique
    • I can use an iterable to add multiple items at once with the update method. I can pass a list for example, and it will add all the unique values to the list. myset.update([44, 777, 44, 56, 89, 777])
    • I cannot have mutable / changeable data as elements of set. No lists, no dicts etc. Strings, Numbers are ok
    • While sets look like lists, they are unordered with no indexing.
    • I can iterate over them, though.
    • To look for something in a set, use the in operator
    • To remove the item use the remove method
    • I can do set operations. s1 - s2 will show me items that are unique to s1. (it removes the common elements between the sets) or vice versa
    • the symmetric_difference method will show me unique elements from both sets. stripping off the common elements. s1.symmetric_difference(s2)
    • to find if a set is a subset of another I could use the <>= operators. Is s2 a subset of s1? s2 < s1
    • I can combine sets (union) s1|s2
    • Or I can find the common elements with intersection s1 & s2

Learning / Feedback/ Experiences from doing exercises

  • Reuven keeps going, Alright? And I keep nodding my head here :)
  • Still miss lots of stuff, writing code. Missing colons, no surrounding quotes, missing commas, so many commas … not converting strings to integers and then wondering how I get such large results from simple addition
  • But I’m done!
  • These were the basics, and I could run through them, but I did not realise I had so many gaps in my basics.
  • As I keep saying Reuven is amazing. He points out the pitfalls and keeps reminding me of important issues and shows various methods to solve things, keeps encouraging me and I could go on and on. Lerner fan for life!

Read all about my Python basics journey here.