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']
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']
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']
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'
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']
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']
numbers = ['one', 'two', 'three', 'four']
numbers.pop()
print(numbers)
>> ['one', 'two', 'three']
numbers.pop(1)
print(numbers)
>> ['one', 'three']
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
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']
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']
numbers = ['one', 'two', 'three', 'four']
numbers.sort(reverse = True)
print(numbers)
>> ['two', 'three', 'one', 'four']
def length(e):
return len(e)
numbers = ['one', 'two', 'three', 'four']
numbers.sort(key = length)
print(numbers)
>> ['one', 'two', 'four', 'three']

Written by

Hi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store