Lists
The main characteristics of a list are:
-
Defined by square bracts, and each element in the list is separated by a comma
-
Collection fo elements that can be of any data type
-
Iterable object: can be divided in smaller parts
-
Mutable Object: which means that can be modified
-
Orderd: ammit the repetiton of elements and can be sliced and indexed
Concatenation and Replication
Concatenation Operator (+): This operator is used to concatenate two or more lists into a single list. For example:
Repetition Operator ( * ) : This operator is used to repeat a list a specified number of times. For example:
Comparison Operators
In Python, you can compare two lists using comparison operators. The comparison operators return a Boolean value (True
or False
) depending on whether the comparison is true or false.
The comparison operators available for lists are:
==
(equal to): returnsTrue
if the two lists have the same elements in the same order, andFalse
otherwise.!=
(not equal to): returnsTrue
if the two lists are not equal, i.e., they either have different elements or the same elements in a different order, andFalse
otherwise.<
(less than): returnsTrue
if the first list comes before the second list lexicographically, i.e., if the first mismatching element is less than the corresponding element in the second list, andFalse
otherwise.>
(greater than): returnsTrue
if the first list comes after the second list lexicographically, i.e., if the first mismatching element is greater than the corresponding element in the second list, andFalse
otherwise.<=
(less than or equal to): returnsTrue
if the first list comes before or is equal to the second list lexicographically, i.e., if the first mismatching element is less than or equal to the corresponding element in the second list, andFalse
otherwise.>=
(greater than or equal to): returnsTrue
if the first list comes after or is equal to the second list lexicographically, i.e., if the first mismatching element is greater than or equal to the corresponding element in the second list, andFalse
otherwise.in
: returnsTrue
if all the elements in the first list are present in the second list, andFalse
otherwise.not in
: returnsTrue
if any of the elements in the first list are not present in the second list, andFalse
otherwise.
Here are some examples:
Note:
- When comparing lists, Python compares the elements of the lists in a pairwise manner, from left to right.
- If the first mismatching element is found, the comparison stops and the corresponding result is returned.
- If the end of either list is reached without finding a mismatch, the shorter list is considered lexicographically smaller.
Built-in Functions
Python offers numerous built-in functions for working with lists. Here are some of the most commonly used functions:
Getting a list length
len()
Adding Values
append()
append
adds an element to the end of a list
:
insert()
insert
adds an element to a list
at a given position:
Removing Values
del()
del
removes an item using the index:
remove()
remove
removes an item with using actual value of it:
Note:
If the value appears multiple times in the list, only the first instance of the value will be removed.
pop()
By default, pop
will remove and return the last item of the list. You can also pass the index of the element as an optional parameter:
Sorting values
sort()
You can also pass True
for the reverse
keyword argument to have sort()
sort the values in reverse order:
If you need to sort the values in regular alphabetical order, pass str.lower
for the key keyword argument in the sort() method call:
You can use the built-in function sorted
to return a new list:
Slicing of a List
Note:
Also, slicing a list does not modify the original list - it returns a new list that contains only the specified portion of the original list.
You can slice a list in Python using the same syntax list[start:end:step]
as you would use for slicing a string, where:
start
is the index of the first element you want to include in the sliceend
is the index of the first element you want to exclude from the slicestep
is the number of elements to skip between each element included in the slice
Here are some examples:
Note:
Like with strings, you can mix positive and negative indices in a single slice, and you can omit any of the three parameters (
start
,end
, andstep
) as needed.
Quartz Index Exampleof a List
In Python, a list is a collection of elements, and you can access individual elements in the list by their position or index within the sequence.
- The index tarts at 0,
- The second character has an index of 1, and so on.
To access a specific element in a string, you can use its index within square brackets []
, like this:
Note:
The index must be an integer, and it must be within the range of valid indices for the string (from 0 to len(lits) - 1
). Otherwise, a IndexError
will be raised.
You can also use negative indices to access elements from the end of the list:
To find the index of a particular element in a list, you can use the index() method, just like with strings:
In this example, we define a list my_list
and an element element
, and then use the index()
method to find the index of the first occurrence of element
within my_list
. The result is 1, which is the index of the second element in the list.
Note:
If the element element
is not found in the list, a ValueError
will be raised.
Modify an element using the index
you can also use the index operator []
to modify an element in a list. Here’s an example:
Loops in a List
In Python, a loop is a programming construct that allows you to iterate over a sequence of items, such as the characters in a string.
There are two types of loops in Python: the for
loop and the while
loop.
-
we use a
for
loop to iterate over each character in the string.
In Python, you can use loops to iterate over the elements in a list. There are two main types of loops you can use: for
loops and while
loops.
For Loops
-
we use a
for
loop to iterate over each element in the list-
You can also use the
range()
function to iterate over a range of indices in the list:
-
While Loops
-
while
loop is designed to repeat a block of code while a specific condition is true