Mastering Python Data Structure (Lists)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Mastering

Python Data Structure


Lists

Cahya Alkahfi
Mastering Python Data Structure Lists by Cahya Alkahfi

Table of contents

1. Lists and its operations

What is a list? Adding elements

Creating a list Modifying elements

Accessing elements Deleting elements

Slicing list Reordering elements

Iterating through list Checking elements

2. Built-in functions that work with lists

len and sum filter

min and max zip

reversed and sorted all and any

map

3. List comprehension
Mastering Python Data Structure Lists by Cahya Alkahfi

What is a list?

In Python, a list is a data type that serves as a collection of elements. Lists


are defined by enclosing elements within square brackets [ ], and
elements are separated by commas. Lists are highly dynamic data
structures that can hold elements of any data type, including numbers,
strings, objects, or even other lists.

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

More complex lists

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

Accessing elements (1/3)

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

Accessing elements (2/3)

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

Accessing elements (3/3)

In a list of dictionaries, we can access elements in the same way. We can


further access any key value from its 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

Iterating through list

List elements can be accessed sequentially through a for-loop iteration.


During each iteration, we can access the list elements directly. We can
also access the index (counter) and value of each element by using
enumerate function
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

Modifying list elements can be done in the same way as accessing or


slicing those elements. Select the element based on the desired index
and assign a new value to it
Mastering Python Data Structure Lists by Cahya Alkahfi

Deleting elements

Deleting an element at a specific index can be done using the pop


method. If we want to remove based on a specific value, then we use the
remove method.
Mastering Python Data Structure Lists by Cahya Alkahfi

Reordering elements (1/2)

The order of elements in a list can be rearranged using reverse or sort


methods.

reverse: method to reverse the order of the elements.


sort: method to sort the elements in ascending or descending order
Mastering Python Data Structure Lists by Cahya Alkahfi

Reordering elements (2/2)

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

count: method to count how many occurrences of a specific value exist


in a list

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 & sum

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 & max

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

reversed & sorted

reversed: function to reverse the order of the elements (similar to


reverse method, but the reversed function doesn't change the original
list and It returns a new iterator object)

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)

The map function is used to apply a given function to each element of a


list (or other iterable objects) and returns a new iterable with the results.
It allows us to perform the same operation on each element without the
need to write a loop explicitly.
Mastering Python Data Structure Lists by Cahya Alkahfi

map (2/2)

The map function can be used to extract specific information from


complex elements within a list.
Mastering Python Data Structure Lists by Cahya Alkahfi

filter

The filter function selects elements within an iterable based on specific


criteria. It takes two arguments: the first argument is a function
containing the filter criteria, and the second argument is the iterable to
be filtered.
Mastering Python Data Structure Lists by Cahya Alkahfi

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 + map example:

zip + filter example:


Mastering Python Data Structure Lists by Cahya Alkahfi

zip (2/2)

zip with more than 2 list example:

zip + map example:


Mastering Python Data Structure Lists by Cahya Alkahfi

all and any

all: returns True if all elements in an iterable are True; otherwise, it


returns False.

any: returns True if at least one element in an iterable is True; otherwise,


it returns False.
Mastering Python Data Structure Lists by Cahya Alkahfi

List comprehension

List comprehension is a concise and powerful feature in Python that


allows us to create lists with a compact and readable syntax. It enables
us to generate new lists by applying expressions to each item in an
existing iterable, such as a list or a range of values. This approach is often
more efficient and expressive than traditional for-loops. List
comprehensions make code cleaner and easier to understand, facilitating
the creation and transformation of lists in a more intuitive and elegant
manner.

There are three parts in a list comprehension:


existing_iterable: the iterable object that will be used in the creation
of a new list.
expression: the code used to process each element from the
existing_list to generate new elements.
condition (optional): conditions can be set to filter elements from the
existing_list that meet specific criteria.

new_list = [expression for item in existing_iterable if condition]

Example:
Mastering Python Data Structure Lists by Cahya Alkahfi

Mapping iterable using list comprehension (1/2)

List comprehension can be used as a replacement for the map method


with a more concise syntax.

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

Mapping iterable using list comprehension (2/2)


Mastering Python Data Structure Lists by Cahya Alkahfi

Filtering iterable using list comprehension

List comprehension can also be used as a replacement for the filter


function.

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

We can perform mapping and filtering simultaneously using list


comprehension.

map + filter

list comprehension
THANK YOU

You might also like