July 21st, 2021
Q: What lectures will we need for Assignment 5?
A1: Up through 15 :) The warmups might need the string parsing lectures
Q: is dict like map, unordered_map in C++ and HashMap in Java?
A1: Yep!
Q: what is the average time complexity for accessing an element in dict?
A1: Average is O(1) and Amortized Worst Case is O(n). More info here: https://wiki.python.org/moin/TimeComplexity
Q: So we only put a space after the colon and not before?
A1: Yes :)
Q: so dictionaries are mutable? you can changes the values assigned originally?
A1: Yes, dictionaries are mutable. An important note is that the keys must be immutable. The values can be mutable or immutable types
Q: does the key have to be a string, can it be a variable?
A1: The key does not have to be a string but it has to be an immutable type (int, string, float etc). I don’t think the key can be a variable
Q: is it possible to store a dcitionary in another dictionary?
A1: Oh yes! We’re going to dive deep into nested dictionaries :)
Q: Can you create the dictionary with initial keys?
A1: Like keys and no values? I don’t think so but you could set the value to be the empty string/list/dict etc.
Q: So you only use the curly bracket when creating the empty dictionary?
A1: Yes :) Empty quotes for strings, Empty Sqaure Brackets for lists, and Empty Curly Braces for dictionaries
Q: can you use d[‘Greta’] ++ like you would in c++ if you wanted to increase the value by 1 or is d[‘Greta’] += 1 the only way?
A1: No. ++ does not work in Python unfortunately. You have to use +=1
Q: what is the complexity for in?
A1: k in d average is O(1) and Amortized Worst Case is O(n). More info here: https://wiki.python.org/moin/TimeComplexity
Q: in def bad_start(meals): meals would be a dictionary right that you pass in?
A1: Yep :)
Q: can you write return ‘breakfast’ not in meals or (‘breakfast’ in meals and meals[‘breakfast’] == ‘candy’ all in one line?
A1: This functionally works but that is a very dense line of code. Stylistically it is better to break it into parts :)
Q: will hw 5 be posted today?
A1: Yes, later tonight :)
Q: is there a set in python?
A1: It does but we don’t cover it in 106A. More info: https://realpython.com/python-sets/
Q: Is either method preferred stylistically? Either having both if statements in one line with an ‘and’ vs having 2 if statements with one nested
A1: I like using the and operator rather than having nested if statements. But both work and sometimes one is “better” than the other.
Q: i did this in my solution if 'lunch' in meals: return meals['lunch']=='candy' is it okey to, regarding style etc… thanks :)
A1: Yeah that looks fine to me :) As a generally rule, you want your code to be readable. So very long lines of code are dense and hard to read. But this still has some decomp/separation of ideas
Q: i forgot the question mark sorry!
A1: No worries! I make so many typos
Q: Why would you choose to use a dictionary over a list?
A1: Great question! Dicts and lists both remember things. The main different is that dicts can have varying keys (strings, ints, floats etc). The “keys” in a list is always the index numbers. So dicts are like more flexible lists :)
Q: can the value be dictionary?
A1: Absolutely! We are going to dive deep into nested dictionaries :) Super useful
Q: is list more reliable and effiecent than dict
A1: I wouldn’t say that one is necessarily better than the other. They are both super useful and important parts of your coding toolbox. They are also both not slow
Q: Can you delete an value /key of a dict?
A1: Did a Google search. Yes, you can use del dict[“key”] but I have never used this before
Q: what is the point of using a list if I just make all the keys integers in a dict?
A1: In this example (str_count1) the values are integers. The keys are words (strings) so this could not be implemented with a list. Please follow up if I did not accurately answer your question :)
Q: what is counts[s]? how are we changing the values and not the keys?
A1: Counts is a dictionary. S is a string. The keys in the counts dictionary are strings and the values are ints. dict[key] = value updates the value associated with a key. Please follow up if you have more questions :)
Q: follow up question to the one regarding dict and list. Isn’t list kind of useless because I can basically just do the exact same thing with a dict with integer keys?
A1: You could implement the same list functionality with a dict, but in that case, I would argue that it would be easier to use a list. Lists are great for just holding a list of items. Dictionaries are great for associating some data with another batch of data. But, I view dicts vs lists as the same idea of a for range loop vs for each loop. Yes, a for range loop is more powerful than a for each loop, but you don’t always need all that functionality. A for each loop is simpler to use and sometimes has all the functionality that you need :)
Q: Are you able to save multiple values to one key in the same dictionary?
A1: Great question! Every key and only be associated with one value. If you want the value to be multiple things you can have the value be a list or a dictionary. Soon (tomorrow I believe) we will go into nested dictionaries (dictionaries where the values are more complex than a string or int).
Q: Could you post the Q & A for lecture 15?
A1: Yes, I will let Juliette know that it’s not posted :) Thanks for letting us know
Q: Follow up to the last question about list vs dict: I did a bit of research into list vs dict The time complexity for dict is average O(1), but in worst case it is O(n), but for a list it will always be O(1). Also a dict uses much more memory than list. Therefore, if one can avoid using dict by using list, one should always use list.
A1: Juliette just added that lookups are always faster in a dictionary :)
A2: Yes, when I can use a list I always try to use a list instead :)
Q: Not related to the lecture, but how do you learn to type faster when coding?
A1: Experience :)
Q: Hi! I hope you are good today! :) Can a key point to a code statement like an if statement or does it have to be a string or an integer?
A1: No sadly. The value has to be a immutable or mutable type
Q: Unrelated, but Juliette is such a French name, can she speak French?
A1: I can only speak some french. My parents love France. They moved there for four years while I was in college.
Q: Can you put grids and booleans as values in a dict?
A1: You can put both grids and booleans as values in a dictionary
Q: What is karel?
A1: Karel is like Bit but an older version.
Q: Is there section for 106B?
A1: Yes. We love section in our CS courses
Q: Awesome! section is awesome
A1: We love section too :)