Dictionaries
In Python, a dictionary is a collection of key-value pairs. It is an unordered, mutable, and indexed collection of elements. Dictionaries are also sometimes referred to as “maps”, “hash maps”, or “associative arrays” in other programming languages.
The main characteristics of a list are:
- Defined by curly braces {}, and colons are used to separate keys and values.
- Iterable object: can be divided in smaller parts, and you can use loops
- Mutable Object: which means that can be modified
- Python Unordered Objects: can’t be sliced or indexed
- Collection of element (values) associated to keys
Nota:
- keys can only be immutable objects, such as: integers, floats, strings, tuples
- Values can be any type of object, such as integers, floats, strings, booleans, as well as more complex data types such as lists, tuples, and even other dictionaries.
Example:
Key can't be repeated
Concatenation and Replication
Concatenation Operator (+): This operator is used to concatenate two or more dictionaries into a one. For example:
Repetition Operator ( * ) : This operator is used to repeat a dictionary a specified number of times. For example:
Comparison Operators
Here are some examples:
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 Example of 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