Python Data Structures
1. Lists
Lists are mutable ordered data structures that are represented by square brackets.
my_list = []
They can contain any of the data type i.e int, strings, char, floats, or doubles.
Indexing with Lists
Lists use the zero-based indexing method to retrieve elements from the list, where by the first element is assigned an index zero.
For example:
fruits = ['Apple','Pitch','Berries']
fruits[0]
>>>Apples
fruits[1]
>>>Pitch
Negative indexing can also be used in accessing elements within the list. For instance:
car_models = ['Ford','Toyota','Ferrari']
car_models[-1]
>>>Ferrari
The negative indexing can be used to retrieve the last element in a list easily.
Slicing
Slicing enables us to retrieve multiple elements from our list.
When slicing it's important to note that the lower index is inclusive and the upper index is excluded.
example:
artists = ['Future','Minaj','Drake','Travis']
artists[1:3]
>>>['Minaj','Drake']
From the above example, we were able to retrieve elements from index 1 to index 2.
One can also start from the first index by omitting the lower index and only providing the upper index.
artists = ['Future','Minaj','Drake','Travis']
artists[:3]
>>>['Future','Minaj','Drake']
In this case we were able to retrieve all the elements starting with the first element and stopping at index 2.
Or you can retrieve all the elements starting from the lower index of your choosing and omitting the upper index.
example:
artists = ['Future','Minaj','Drake','Travis']
artists[1:]
>>>['Minaj','Drake','Travis]
In addition to slicing in lists we can also use the in
and not in
methods to check if an element is found in our list or not.
numbers = [1,2,3,4,5,6]
12 in numbers
>>>False
5 in numbers
>>>True
23 not in numbers
>>>True
Mutability In Lists
Unlike strings lists values can be changed.
Deciding whether an object can change the values after it has been created is known as mutability.
you can use indexing to change values of within a list.
example:
cars = ['Volvo','Mercedes','BMW','Audi']
cars[2] = 'Ford'
print(cars)
>>>['Volvo','Mercedes','Ford','Audi']
Useful Functions Used With Lists
len()
: returns the number of elements in the list
example:
cars = ['Volvo','Mercedes','BMW','Audi']
len(cars)
>>>4
max()
: returns the greatest element of the list.
The greatest element in a list is determined by what type of objects are in the list. For a case of numbers the max()
function will return the highest number in the list, while in the case of Strings the max()
function will return the element that would appear last in the list if the list was sorted alphabetically.
min()
: returns the smallest element in a list, min() function is a polar opposite of the max function.
sorted()
: returns a copy of the list in order from smallest to largest, leaving the list unchanged.
cars = ['Volvo','Mercedes','BMW','Audi']
print(sorted(cars,reverse=False))
>>>['Audi','BMW','Mercedes','Volvo']
append()
: adds elements into the list
desert = ['biscuits', 'cakes', 'cookies', 'custards', 'gelatins', 'ice creams']
desert.append(['pudding'])
print(desert)
>>>['biscuits', 'cakes', 'cookies', 'custards', 'gelatins', 'ice creams', ['pudding']]
Top comments (0)