DEV Community

Cover image for PYTHON LISTS
pank-guru
pank-guru

Posted on

PYTHON LISTS

Python Data Structures

1. Lists

Lists are mutable ordered data structures that are represented by square brackets.

my_list = []
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Negative indexing can also be used in accessing elements within the list. For instance:

car_models = ['Ford','Toyota','Ferrari']
car_models[-1]
>>>Ferrari
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

Useful Functions Used With Lists

len() : returns the number of elements in the list
example:

cars = ['Volvo','Mercedes','BMW','Audi']
len(cars)
>>>4
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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']]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)