Mastering Python Data Structure (Lists)
Mastering Python Data Structure (Lists)
Mastering Python Data Structure (Lists)
Cahya Alkahfi
Mastering Python Data Structure Lists by Cahya Alkahfi
Table of contents
map
3. List comprehension
Mastering Python Data Structure Lists by Cahya Alkahfi
What is a list?
mutable: lists are mutable which means we can modify, add, or remove
elements from a list.
ordered: the elements are arranged in a specific sequence, and this order
is maintained when accessing the list's elements.
duplicate elements: a list can store duplicate elements and the order of
elements is maintained
Mastering Python Data Structure Lists by Cahya Alkahfi
Creating a list
Lists can be created using square brackets [], with each element written
inside them and separated by commas. Lists can also be created using
the list function. This function is typically used to convert other iterable
objects into lists.
Mastering Python Data Structure Lists by Cahya Alkahfi
Lists can hold elements of any data type including lists or dictionaries.
Here are examples of lists of lists and lists of dictionaries.
Mastering Python Data Structure Lists by Cahya Alkahfi
List elements can be accessed based on their index order using the
syntax name_list[index]. Indexing in lists starts from 0. This means the 1st
element will have an index of 0, the 2nd element with an index of 1, and
so on. Lists can also be accessed using negative indexes. Index -1
indicates the last element in the list, index -2 the second-to-last element,
and so forth.
Mastering Python Data Structure Lists by Cahya Alkahfi
In a list of lists, we can access elements in the same way. We can further
access elements from its main elements.
Mastering Python Data Structure Lists by Cahya Alkahfi
Slicing list
A list can be sliced to take only a few elements. Slicing a list is done using
a starting index and an ending index. The starting index is inclusive, while
the ending index is exclusive. For example, to slice the first to the third
element, the starting index is 0, and the ending index is 3. We can omit
the starting index when slicing from the first element, and likewise, we
can omit the ending index when slicing to the last element. Slicing can
also be performed using negative indices.
Mastering Python Data Structure Lists by Cahya Alkahfi
Adding elements
Adding elements to a list can be done using the append method, insert
method, or adding with another list object.
Mastering Python Data Structure Lists by Cahya Alkahfi
Modifying elements
Deleting elements
When using the sort method, we can customize how the elements are
sorted by specifying the key parameter.
Mastering Python Data Structure Lists by Cahya Alkahfi
Checking Elements
index: method to find the first index that contains a specific value
in (not in): operator to check whether one or more elements exist in a list
Mastering Python Data Structure Lists by Cahya Alkahfi
len: function to get the number of elements in a list (or other iterable)
sum: function to get the sum of values in the elements of a list (or other
iterable)
Mastering Python Data Structure Lists by Cahya Alkahfi
min: function to get the element with the smallest value from a list (or
other iterable)
max: function to get the element with the largest value from a list (or
other iterable)
The min and max functions have a `key` parameter where we can pass a
function how the smallest or largest values are measured. This allows us
to find the minimum and maximum values in a list with more complex
elements
Mastering Python Data Structure Lists by Cahya Alkahfi
sorted: function to sort the elements (similar to sort method, but the
sorted function doesn't change the original list and It returns a new
iterator object)
Mastering Python Data Structure Lists by Cahya Alkahfi
map (1/2)
map (2/2)
filter
zip (1/2)
The zip function is used to combine elements from one or more iterables
into a set of paired tuples. This merging allows us to perform paired
operations between elements at the same positions in each list. This
function can accommodate two or more iterables at once. If the number
of elements in each iterable is different, the zip function will stop after
the shortest iterable.
zip (2/2)
List comprehension
Example:
Mastering Python Data Structure Lists by Cahya Alkahfi
The following examples are syntax for performing mapping using list
comprehensions and their comparison with the map function.
Mastering Python Data Structure Lists by Cahya Alkahfi
The following examples are syntax for performing filtering using list
comprehensions and their comparison with the filter function.
Mastering Python Data Structure Lists by Cahya Alkahfi
Mapping + Filtering
map + filter
list comprehension
THANK YOU