We learned how to use List comprehensions in python. List comprehensions provide a concise way to create lists. As list comprehensions returns lists, they consist of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element. we saw some examples of how to create a list using the list comprehensions technique, for example sqr numbers: 2, 4, 9, 16, 25 … We studied a new collection type called dictionary. Each item of the dictionary consist of a key and a value pair (also called entry). dictionary is used whenever you want to quickly find an item by it’s key i.e. person-id as a key and the person-name as a value. You can access the items of a dictionary by referring to its key name, inside square brackets. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}. dictionaries are stored in a JSON format. you can retrieve all of the keys of the dictionary by using the keys( ) method and retrieve all of the values of the dictionary by using the values( ) method. dictionary keys are unique, which means that writing a value into the dictionary using an existing key will overwrite the existing entry. we wrote some helper functions to work with the dictionary i.e. tryAddValue which adds an entry to a given dictionary only if the given key does not already exist in a the dictionary
More topics covered:
- String concatenation
- String Slice
- JSON format
- XML format
- List comprehension with condition
- List comprehension using range
- dictionary pop
- dictionary remove item
- dictionary print
- dictionary type is called dict
- for loop with 2 parameters
Links:
- Class code: string concatenation
- Class code: string slice
- Class code: list comprehension
- Class code: dictionary promo
- Class code: dictionary
- Class code: dictionary – more…
- List Comprehension – nice article
- Dictionary – w3schools
- What is JSON?
- What is XML?
- String slice – nice article
- Home Work!
- Lesson Summary Video!
