3) h. Common Usage Methods across all Data Structures

Even though the data structures described earlier are quite different from each other they do have common usages and techniques.

First of all checking if a data structure is empty is very easy. Any empty container is interpreted as False and non-empty one True .

1
2
Output:
Oh no. Who ate all the cookies?



Also quite often you want to check if a certain value is in a container.

1
2
Output:
Someone forgot to put oranges.

Note that in this example you only check if “Oranges” is present as a key of the basket dictionary. Even if “Orange” was 0 the check would pass.

Methods:

Take a look at each method and try to guess the output based on the method’s name. See how many you can guess correctly.

a) all(iterable):

Return whether all elements of iterable evaluate to true.
1
2
Output:
False



b) any(iterable):

Return whether any elements of iterable evaluate to true.
1
2
Output:
True



c) enumerate(iterable, start=0):

Return an enumerate object. When iterating over this object elements position and the element itself is yielded. Optional parameter start specifies where to start counting.
1
2
3
4
5
Output:
(0, 'Tolkien')
(1, 'Wells')
(2, 'Rowling')
(3, 'Wilde')



*d) filter(function, iterable):

* Using this method involves the concept of functions/lambda functions. This is provided here just for you to know. If you’re not acquainted with functions then ignore this section. We will come back to this again in lambda functions. Return an iterator from elements of iterable which make function return true.
1
2
3
Output:
['Frank', 'Eric']
['Frank', 'Eric']



e) len(iterable):

Return number of elements in iterable.
1
2
Output:
6



f) map(function, iterable, …)

Return an iterator that applies function to the elements of one or more provided iterables in parallel. If iterables have different sizes, returned iterator stops when the shortest iterable is exhausted.
1
2
Output:
[6, 5, 4, 7]



g) max(iterable, *[,key, default])

Return the largest element of iterable. Optional key parameter can be used to specify the comparison function. Optional default parameter can be used to specify a default return value if iterable is empty.
1
2
Output:
94



h) min(iterable, *[,key, default])

Return the smallest element of iterable. Optional keyparameter can be used to specify the comparison function. Optional default parameter can be used to specify a default return value if iterable is empty.
1
2
Output:
36



i) reversed(sequence):

Return a reverse iterator for the provided ordered sequence.
1
2
Output:
[10, 8, 6, 4, 2]



j) sorted(iterable, *, key=None, reverse=False):

Return a sorted list made of elements from iterable. Optional key parameter can be used to specify the comparison function. Optional reverse parameter can be used to specify whether the order should be reversed.
1
2
Output:
['pineapple', 'banana', 'apple', 'pear']



k) sum(iterable, start=0):

Return a sum of start that is 0 by default and values of the elements of iterable.
1
2
Output:
30



l) zip(*iterables):

Return an iterator that combines elements from each of the provided iterables.
1
2
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]