Data Structures are ways to organize data in Python so that it is easier to read for computers. Types of data structures are Sequences (list, tuples, and strings), Sets (a set, and a frozen set), and Maps (dictionaries). There are different ways within Python to use these data structures that can make programming easier. Data structures make it easier to access data as well by sorting list, using list comprehension and generator expressions and iterating through dictionaries, just to name a few.
Sequences
Sequences are most used in Python. Sequences are usually in a specific order and can be accessed using their designated index. A programmer should always use sequences when data is connected in any way to each other. There are also built in sequence methods that can be used for iterations, algorithms, etc. As mentioned earlier there are different ways to access data using data structures, for sequences there are ways to sort, add, clear, and remove data from these sequences.
METHODS/OPERATIONS
Sorting
Python has a number of different built in methods and operations. For sorting a list, which is a sequence, we can use the built in Python methods such as .sort()
or sorted()
. These methods can only be used on elements that consist of the same data type. Using the sorted()
method we can create a sorted copy of our original list or sequence, we can even make the method more specific by using a key or designating whether the result needs to be reversed or not. Using the .sort()
method rearranges the list to be in order without copying the list.
list = [a, a, e, d]
list.sort()
print(list)
#[a, a, d, e]
list_two = [1, 1, 4, 2, 3, 5]
sorted_list = sorted(list_two, reversed=True)
print(sorted_list)
#[5, 4, 3, 2, 1, 1]
print(list_two)
#[1, 1, 4, 2, 3, 5]
Adding
We can also modify sequences by adding to the original sequence using .append()
to add at the end of the sequence or .insert()
to insert at a given index. .insert()
accepts two parameters which is the index of where the data will be placed and the value itself. .append()
accepts one parameter which is the value that will be added to the sequence. In cases where that the index provided for the .insert()
method does not exist, the value will be placed at the end by default. If a value exist at the index then the new value will be placed before it.
list = [1, 2, 3, 4]
list.append(5)
print(list)
#[1, 2, 3, 4, 5]
list_two = [1, 2, 4, 5]
list_two.insert(2, 3)
print(list_two)
#[1, 2, 3, 4, 5]
list_two.insert(10, 6)
print(list_two)
#[1, 2, 3, 4, 5, 6]
Removing
There are a few methods that can be used to remove an element from list such as del()
, which removes elements by a specified index or a range of indices, .pop()
, which when used with an argument it will remove and return the element at a given index; without an argument it will remove the element at the last index, .remove()
, which will remove an element’s value that is passed in as an argument and .clear()
, which erases the entire list.
list = [1, 2, 3, 4, 5, 6, 7, 8]
del(list[3])
#[1, 2, 3, 5, 6, 7, 8]
list.pop()
#[1, 2, 3, 5, 6, 7]
list.pop(2)
# 3
list.remove(1)
print(list)
#[2, 5, 6, 7]
list.clear()
print(list)
#[]
Ranges
Ranges can often be used in for
loops with the syntax of range()
. It is used to return elements from a list, it takes in one argument which will be the stop value but can also accept two other optional arguments which can include the start value and a step size. The default step size is 1 if only one argument is passed.
for x in range(2):
#stop value is passed as an arguement with 1 as the default step size.
print(x)
# 0
# 1
# 2
for x in range(1, 2):
#start and stop value is passed with 1 as the default step size.
print(x)
# 1
for x in range(0, 3, 2):
#start, stop and step size is passed as an argument.
print(x)
# 0
# 2
Using this for helpful tools for data structure sequences can make life as a programmer more beneficial and a lot easier. The built in methods from Python were formulated specifically to make retrieving and modifying data a simpler task.