Python List

Daniel Liu
4 min readAug 20, 2020

Creating

Lists are similar to arrays in other languages. Like arrays you can make a list using square brackets. List is a reserved word so it is not recommended to use list as a variable.

numbers = ['one', 'two', 'three']
print(numbers)
>> ['one', 'two', 'three']

Another way to create a list is using the list method.

numbers = list(('one', 'two', 'three')) # Make sure to use double parenthesis
print(numbers)
>> ['one', 'two', 'three']

Indexing

To access elements you use the index similar to arrays. Like arrays indexes start from 0. If you provide an index out of range it will throw an error.

numbers = ['one', 'two', 'three']
print(numbers[0]) # Prints the first element in the list
>> 'one'

Negative Indexing

Unlike most languages you can use negative indexes. -1 would be the last element, -2 would be the second last. Basically you would go backwards starting from -1.

numbers = ['one', 'two', 'three']
print(numbers[-1]) # Prints last element in the list
>> 'three'

Range Index

You can access sublists by providing a range. The first element is where it starts and the last is where it ends but does not include the end.

numbers = ['one', 'two', 'three', 'four']
print(numbers[1:3]) # Prints lists from index 1-3 not including index 3
>> ['two', 'three']

You can leave out either the start or last range. If you leave out the start it will include everything from the beginning. If you leave out the end it will include everything to the end.

numbers = ['one', 'two', 'three', 'four']
print(numbers[:3]) # Print everything from beginning to index, not including, 3
>> ['one', 'two', 'three']
print(numbers[1:]) # Print everything from index 1 to the end
>> ['two', 'three', 'four']

You can use negative index range as well.

numbers = ['one', 'two', 'three', 'four']
print(numbers[-3:-1]) # Prints 2rd last to but not including last
>> ['two', 'three']

Update

To change the value of the list refer to the index and provide it a new value.

numbers = ['one', 'two', 'three', 'four']
numbers[1] = 'five'
print(numbers)
>> ['one', 'five', 'three', 'four']

Iterate List

To iterate through a list you can use a for loop.

numbers = ['one', 'two', 'three', 'four']
for x, index in numbers:
print(x, index)
>> 'one'
>> 'two'
>> 'three'
>> 'four'

To get the index as well you would need to use the enumerate method as well.

numbers = ['one', 'two', 'three', 'four']
for index, x in enumerate(numbers):
print(index, index)
>> 0, 'one'
>> 1, 'two'
>> 2, 'three'
>> 3, 'four'

Check Value

To check if a value exists in the list you would use the in keyword.

numbers = ['one', 'two', 'three', 'four']
print('zero' in numbers)
>> False
print('one' in numbers)
>> True

Length

To get the length of the list you would use the len method.

numbers = ['one', 'two', 'three', 'four']
print(len(numbers))
>> 4

Add Values

To add values to the end of the list you would use the append method.

numbers = ['one', 'two', 'three', 'four']
numbers.append('five')
print(numbers)
>> ['one', 'two', 'three', 'four', 'five']

To add values to a specific index that is not the end you would use the insert method by providing it the index and value.

numbers = ['one', 'two', 'three', 'four']
numbers.insert(1, 'five')
print(numbers)
>> ['one', 'five', 'two', 'three', 'four']

Remove Values

To remove a specific value from the list you would use the remove method by providing it the value. Keep in mind that if the value does not exist in the list it will throw an error.

numbers = ['one', 'two', 'three', 'four']
numbers.remove('two')
print(numbers)
>> ['one', 'three', 'four']

To remove a value from a specific index you can use pop method or del keyword. If you do not provide a value for pop it will remove the last value in the list.

numbers = ['one', 'two', 'three', 'four']
numbers.pop()
print(numbers)
>> ['one', 'two', 'three']
numbers.pop(1)
print(numbers)
>> ['one', 'three']

If you do not provide an element for the array when using del it will delete the entire list.

numbers = ['one', 'two', 'three', 'four']
del numbers[0]
print(numbers)
>> ['two', 'three', 'four']
del numbers
# Trying to access list will throw an error saying it is not defined

To clear the list you would use the clear method.

numbers = ['one', 'two', 'three', 'four']
numbers.clear()
print(numbers)
>> []

Copying

There are 2 ways to copy a list the copy method and the list method.

numbers = ['one', 'two', 'three', 'four']numbersCopy = numbers.copy()
print(numbersCopy)
>> ['one', 'two', 'three', 'four']
numbersCopy2 = list(numbers)
print(numbersCopy2)
>> ['one', 'two', 'three', 'four']

Joining

There are different ways to combine lists. The easiest way is to use the + operator.

numbers1 = ['one', 'two', 'three', 'four']
numbers2 = ['five', 'six', 'seven', 'eight']
numbers3 = numbers1 + numbers2
print(numbers3)
>> ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']

Another ways is to use the extend method.

numbers1 = ['one', 'two', 'three', 'four']
numbers2 = ['five', 'six', 'seven', 'eight']
numbers1.extend(numbers2)
print(numbers1)
>> ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']

Reverse

You can reverse the order of the list by using the reverse method.

numbers = ['one', 'two', 'three', 'four']
numbers.reverse()
print(numbers)
>> ['four', 'three', 'two', 'one']

Sort

To sort a list you would use the sort method. By default it will sort in ascending order.

numbers = ['one', 'two', 'three', 'four']
numbers.sort()
print(numbers)
>> ['four', 'one', 'three', 'two']

To make it descending you can pass it the parameter “reverse = True’”.

numbers = ['one', 'two', 'three', 'four']
numbers.sort(reverse = True)
print(numbers)
>> ['two', 'three', 'one', 'four']

To sort it on some other value like length you could create a method and pass it the parameter “key=method”.

def length(e):
return len(e)
numbers = ['one', 'two', 'three', 'four']
numbers.sort(key = length)
print(numbers)
>> ['one', 'two', 'four', 'three']

--

--