A list
is a data type in Python that lets you store linear data. Each item is stored with a numerical index attached to it so that it becomes easily accessible later. A Python list
is defined as comma-separated items inside a pair of ()
.
Here's a quick recap:
- written with
[]
- changeable, ordered, indexed
- duplicates are allowed
- looks something like this:
pythonlist = [10,20,39,'dottedsquirrel.com']
Combining lists
To combine a Python list
, you can use a *
. Here is an example:
a = [1, 2]
b = [3, 4]
c = [*a, *b]
print(c)
# this will return:
# [1, 2, 3, 4]
list.append()
append()
adds an element to the list. Here is an example:
a = [1, 2]
b = [6, 7, 8]
a.append(3)
a.append(b)
print(a)
# this will return:
# [1, 2, 3, [6, 7, 8]]
list.extend()
append()
will simply ‘tack’ on whatever value passed in. However, to properly add integrate one list into another, you should use extend()
. Note that this will only work with iterable values and not single values. Here is an example:
a = [1, 2]
b = [6, 7, 8]
a.extend(b)
print(a)
# this will return:
# [1, 2, 6, 7, 8]
list.clear()
clear()
will remove all items from the list in Python. Here is an example:
a = [1, 2]
a.clear()
# this will return
# []
list.copy()
copy()
will create a copy of a list in Python. Here is an example:
a = [1, 2]
b = a.copy()
print(b)
# this will return
# [1, 2]
list.count()
count()
will count the number of times a value appears in a list. Here is an example:
message = ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
countme = message.count('l')
print(countme)
# this will return
# 3
list.index()
index()
will find the index position of a value in a list. Lists start their index at 0
. It is also good to note that it will stop at the first instance of the matched value. For example:
message = ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
indexme = message.index('l')
print(indexme)
# this will return
# 2
list.insert()
Use insert()
if you want to add a value into a specific position inside your list rather than append it at the end. For example:
message = ['H', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
message.insert(1, 'e')
print(message)
# this will return
# ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
list.remove()
You can remove a specific item from the list by using remove()
. Note that, similar to index()
, it will only remove the first instance of the matched value. To remove all matched values, you’ll need to put them through a loop. For example:
letters = ['a', 'b', 'f', 'c', 'f']
letters.remove('f')
print(letters)
# this will return
# ['a', 'b', 'c', 'f']
Here’s an example of a loop to remove all values inside a list. We pair this with range()
to know how many times it appears.
letters = ['a', 'b', 'f', 'c', 'f']
for item in range(letters.count('f'))
letters.remove('f')
list.pop()
pop()
will eject the last item from the array. For example:
letters = ['a', 'b', 'f', 'c', 'f']
letters.pop()
print(letters)
# this will return:
# ['a', 'b', 'f', 'c']
If you try to pop an empty array, you will get an IndexError
list.reverse()
reverse()
will do as it says — reverses the list. For example:
letters = ['a', 'b', 'f', 'c', 'f']
letters.reverse()print(letters)
# this will return
# ['f', 'c', 'f', 'b', 'a']
list.sort()
sort()
will rearrange your list to ascending order. It’s good to note that character order is based on the ASCII table.
sortme = ['c','a','d','b']
sortme.sort()
print(sortme)
# this will return:
# ['a', 'b', 'c', 'd']
And that's basically it.