Working With Lists - Ipynb - Colab
Working With Lists - Ipynb - Colab
ipynb - Colab
To start working on this notebook, or any other notebook that we will use in this course, we will
need to save our own copy of it. We can do this by clicking File > Save a Copy in Drive. We will
then be able to make edits to our own copy of this notebook.
moringa_address_list
keyboard_arrow_down Objectives
You will be able to:
What is a list?
keyboard_arrow_down
Syntax:
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 1/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
list_name[index]
Indexing in lists refers to the process of accessing or retrieving elements from a list based on
their position or index. The index of a list starts from 0 for the first element, 1 for the second
element, and so on.
For instance, if we have a list named my_list , and we want to access the element at index 2, we
would use the syntax:
my_list[2]
This method allows us, as programmers, to precisely retrieve and work with individual elements
within a list based on their positions.
Negative indexing starts from the end. The last element has an index of -1 , the second-
to-last has an index of -2 , and so on.
list_name[start:stop:step]
start : The starting index of the slice. It represents the first element you want in the sliced
list.
stop : The stopping index (exclusive) of the slice. It represents the first element you do not
want in the sliced list.
step (optional): The step or increment between elements. If not specified, it defaults to 1.
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 2/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
# example list
Syntax:
list_name[index] = new_value
# example list
fruits = ["strawberry", "pineapples"]
print(fruits)
['strawberry', 'pineapples']
['oranges', 'pineapples']
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 3/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
elements, enabling us to seamlessly integrate fresh data into our lists. For instance, we might
need to add new observations to an existing dataset. Python provides intuitive methods for
adding elements to a list such as append() and insert() , fostering the adaptability and growth
of our lists to accommodate evolving data requirements.
append() - is a straightforward and efficient way to add items to the end of a list. By
calling the append() method on a list, we can include a new element, and it will be
automatically placed at the last position. This is particularly useful when we want to
continuously extend a list with additional data.
Syntax:
list_name.append(item)
insert() - provides a flexible way to add an element at a specific position within a list.
Unlike append() , which adds the element at the end, insert() allows us to choose the
index where the new element should be placed.
syntax:
list_item.insert(index, item)
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 4/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
pop() - removes the last item from a list, providing the advantage of retaining and
allowing us to work with that removed item. Consider a list as a deck of cards, and using
pop() is akin to taking the top card off the deck. In this analogy, the top card corresponds
to the end of the list, and by popping it off, we can manipulate or examine that card
separately.
syntax:
list_name.pop()
pop(index) - allows us to remove and retrieve an item from a specific position in a list,
specified by the provided index. This method is beneficial when we want to precisely target
and work with an element from a non-terminal position within the list.
Syntax:
list_name.pop(index)
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 5/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
remove() - designed to eliminate a specific value from a list. Unlike the pop() method,
which removes an element based on its index, remove() targets an element by its actual
value. This method is particularly useful when we want to delete a specific element
without knowing its position in the list.
Syntax:
list_name.remove(item_value)
Recall
# updated list
# remove Mango
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 6/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
other scenarios, we intend to modify that order. Python equips us with various methods to
organize our lists based on specific situations, offering flexibility in managing the presentation
of data such as sort() and sorted() .
# example
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 7/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
order, meaning the last item becomes the first, the second-to-last becomes the second,
and so on, effectively flipping the list.
We can use the reverse() method to invert the original sequence of a list. For example, if we
initially arranged the list according to the chronological order of car ownership, we can
efficiently reorganize it into a reverse chronological order.
We notice that reverse() doesn't perform a backward alphabetical sort; instead, it reverses the
list's existing order. The method permanently alters the list's sequence, but we can return to the
original order at any time by applying reverse() to the same list a second time.
By using len() with lists, we efficiently determine the quantity of items contained within the list,
enabling us to manage and manipulate the list's size as needed in our programming tasks.
Syntax
len(list_name)
When we use in with a list, it evaluates to True if the element is present in the list and False if
it's not found. This operator provides a straightforward way for us to confirm the existence of
elements within lists, allowing us to make decisions or perform actions based on the presence
or absence of specific values.
When used with a list, not in evaluates to True if the element is not present in the list and
False if it is found.
# example
# updated list
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 9/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
keyboard_arrow_down Tuples
In this section, we'll explore tuples, a fundamental data structure in Python.
A tuple is an ordered collection of elements, similar to a list, but with a crucial difference:
tuples are immutable. Once created, their contents cannot be altered or modified.
Tuples are denoted by parentheses () and can contain various data types, including integers,
strings, floats, or even other tuples.
Syntax:
# example 1
# example 2
Tuples serve exceptionally well in situations where data integrity and immutability are
paramount. For instance, they are often used to store fixed collections of related items, such as
coordinates in geometry, dates and times, or as keys in dictionaries.
Modification Add, remove, and change items Cannot add, remove, or modify items
Performance Slightly slower due to mutability Slightly faster due to immutability
Use Cases Dynamic data, frequently changing elements Fixed data, maintains integrity
Denotation [item1, item2, ...] (item1, item2, ...)
keyboard_arrow_down Summary
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 10/11
2/4/25, 3:37 PM working_with_lists.ipynb - Colab
https://colab.research.google.com/drive/1-0IkaK-QD5fS0hJd39yyPBL-nejhjaCF#printMode=true 11/11