Lists
Code sample link: https://replit.com/@jjoco/python-lists
In Python, lists are used to keep a record of values of a specific data type in an ordered way, each of which can be processed. Lists can contain duplicates of the same value. These can be thought of as arrays in other languages, but more function similarly to ArrayLists
in Java. Python lists start indexing at 0.
Creating a list
To create an empty list, one can use square brackets []
, like so:
1 |
|
One can also type hint a list; to type hint the list of strings, one can write the following
1 2 3 4 5 6 |
|
1 |
|
Unlike strictly typed languages, Python allows lists to contain more than one type. For example, the following multi-type list is valid in Python:
1 |
|
Even if a developer type hints, the multi-type list is still a valid list, like so:
1 |
|
A cool feature is that one can create a list of a certain size, with all values initialized. For example, to create a list of size 7 filled with None
, you can write the following:
1 2 |
|
Iterating through a list
The Python way of iterating through a list is very similar to how other languages use iterator looping.
1 2 |
|
Let's say I wanted to print out the elements in a given list. You can do the following:
1 2 3 4 5 6 7 8 9 |
|
Iterating through a list via enumerate
Sample: https://replit.com/@jjoco/python-enumerate
Python has a special function called enumerate
. enumerate
's parameter is a list, and it returns an enumeration of the list that allows a dev to loop through a list's indicies and elements simultaneously.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
In case the value of an element does not matter, a dev can use an underscore _
in place of the value to denote a dont-care
.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Likewise if the index of an element does not matter, use an underscore in place of the index.
1 2 3 4 5 6 7 8 9 10 11 |
|
Setting a list element
A dev can set the value of an element as a given index like the following.
1 2 |
|
IndexOutOfRangeException
.
Getting a list element
To get an element at a given index, use the following syntax:
1 |
|
my_list = [2, 21, 7, 8, 65 ,9 , 0]
at index 2, we can write:
1 2 |
|
Getting the length of a list
To get the number of elements in a list (ie. its length), use the len
function:
1 |
|
my_list = [2, 21, 7, 8, 65 ,9 , 0]
, then len(my_list)
would be 7
.
Iterating through a list via range
Devs can also iterate through a list via indices, but they still need to use the iterator syntax mentioned earlier using a special function range(n)
. range(n)
returns a sequence of numbers from 0
to n-1
.
To iterate through an entire list using range
, input the length of the list as its parameter.
1 2 3 4 |
|
Appending list element to the end of a list
To append to the end of a list, use the list method append(elem)
function to add elem
to the end of a given list.
1 2 3 4 |
|
Slicing a list
A list can be created from a previously created list by using the following syntax.
Syntax
example_list[:end]
=> gets a list of elements from beginning of exampleList until indexend
(excluding element at index end)example_list[start:]
=> gets a list of elements starting from indexstart
(inclusive) until the end of exampleListexample_list[start:end]
=> gets a list of elements from exampleList starting from indexstart
(inclusive) until the end indexend
(exclusive)
Example:
1 2 3 4 5 6 7 8 9 10 |
|
Popping a list element from end of a list
To remove the last element of a given list (or "pop" it), use the pop()
function. Note that this function also returns the item popped.
1 2 3 4 5 |
|
Extending a list with another list
To extend a list with another list (ie. concatenating the two lists), use the extend
function:
1 2 3 4 5 6 |
|