fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
fruits.count('apple')2
Today, we’re discussing section 5 of The Python Tutorial. The subsections are
del statementThe goal of this section is to get a handle on the different built-in data structures. This does not include pandas dataframes.
A data structure is a variable that contains other variables.
Different data structures have slightly different behaviour,
The main examples that we’ll cover:
| Data structure | Indexing | Ordering | Mutability |
|---|---|---|---|
Lists list |
By position (0, 1, 2, … ) |
Ordered | Mutable |
Tuples tuple |
By position (0, 1, 2, … ) |
Ordered | Immutable |
Sets set |
Not possible | Unordered | Mutable |
Dictionaries dict | By key (key1,key2,key3`, … ) |
Insertion-ordered | Mutable |
Lists, like all Python variables, come with a set of ‘methods’ or functions generic to all lists.
Let’s make an example list and look at a few:
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
These are concise ways to create lists from other lists. They look like loops, but don’t work the same way.
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
That second line has the list comprehension. It makes a new list, populating each element with x**2, each time pulling x from one_to_ten.
You can add a conditional,
You can also nest comprehensions, which gets quite confusing. This one combines x and y into a list if they’re different, making a bigger list of all the combinations.
del statementTo delete elements of a list, or indeed the whole list, you can use the del statement:
Tuples are closely related to lists. The main difference is that they are immutable - you can’t modify elements in-place, or append to them, or delete things. In that sense they are ‘constant’ but don’t be fooled: you can always overwrite them.
To make a tuple, don’t use any brackets (or uses parentheses):
We index them like lists, but cannot modify the elements:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[27], line 1 ----> 1 veg[0] = "potato" TypeError: 'tuple' object does not support item assignment
Empty or one-element lists are a bit strange: “ugly, but effective”:
Set’s are a bit different, they represent the mathematical concept of a set, which is an unordered collection of different things.
Use curly braces, {...}, to create sets.
{'brown', 'multigrain', 'white', 'wholemeal'}
Notice that the order has changed and duplicates have been removed.
We can use the in and not in keywords to check membership (this works for lists and tuples too!)
Sets have special logical operators. Here, a and b are both sets.
| Operator | Name | Description |
|---|---|---|
a - b |
Difference | Letters in a but not in b |
a \| b |
Union | Letters in a or b (or both) |
a & b |
Intersection | Letters only in both |
a ^ b |
Symmetric difference | Letters in a or b but not both |
Set comprehensions also exist:
Dictionaries are formed by key: value pairs between curly braces:
Index them by the keys:
Dictionary comprehensions exist:
For loops are useful for running code on each element in a data structure.
Looping through dictionaries goes through the keys by default:
so use .items() to loop through both:
Notice that we assign to two variables in the for loop.
Use enumerate() to get the index when you loop through a list or tuple:
Loop through two things at once with zip()
A few bits and pieces here.
while and if statements can contain any operators, not just comparisons.in and not in test membership. Operators is and not is test identity.a < b == c tests whether a is less than b and moreover b == cand and or and negated with not.and and or are short-circuit operators: they stop as soon as the outcome is known.You can compare multiple data structures (rather than their elements), using lexographical ordering. If all items are the same, they are equal, and if they are different length, only the length of the shorter one is checked.