DEV Community

Vicki Langer

Posted on • Updated on

Charming the Python: Lists

If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.

Lists

A list is a collection which is ordered and changeable and there may be duplicate items.

Creating a List

``````# creating a list with a builtin function
list_name = list('list item 1', 'list item 2', 'list item 3')

# creating a list with brackets
another_list_name = ['list item 1', 'list item 2', 'list item 3']

# you can create an empty list like this
list = []
``````

Accessing list items using indexing

cat dog mouse cheese lemon road
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']

print(things[0])
>>>cat

print(things[-2])
>>>lemon
``````

Changing Lists

Lists are mutable, or modifiable.

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things[0] = 'chair' # adds 'chair' to the beginning of list, at index 0
print(things)  # returns newly modified list
>>>['chair', 'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']
``````

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.append = 'chair' # adds 'chair' to the end of list
print(things)  # returns newly modified list
>>>['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road', 'chair']
``````

Check

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
does_exist = 'banana' in things  # sees if 'banana' is in the list
print(does_exist)
>>>True
does_exist = 'lime' in things  # sees if 'lime' is in the list
print(does_exist)
>>>False
``````

Insert

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.insert('pie', 1)  # adds 'pie' at index 1
print(things)
>>>'cat', 'pie', 'dog', 'mouse', 'cheese', 'lemon', 'road'
``````

Remove

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.remove('mouse')  # removes 'mouse', regardless of index
print(things)
>>>'cat', 'dog', 'cheese', 'lemon', 'road'
``````

Pop

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.pop(-2)  # removes item referenced by index
print(things)
>>>'cat', 'dog', 'mouse', 'cheese', 'road'
``````

Delete

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
del things[0]  # deletes item referenced by index
print(things)
>>>'dog', 'mouse', 'cheese', 'lemon', 'road'
``````

Clear

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']  # our list
things.clear()
print(things)  # nothing will show because you've cleared all items from the list
>>>
``````

Joining

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']
more_things = ["table", "book", "blanket"]
new_list = things + more_things  # combines both lists in the order written
print(new_list)
>>>'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road', "table", "book", "blanket"

newer_list = more_things + things
>>>"table", "book", "blanket", 'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road'
``````

Counting

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']
print(things.count())
>>>6
``````

Reversing

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']

things.reverse()  # flip the order of the list
print(things)
>>>'road', 'lemon', 'cheese', 'mouse', 'dog', 'cat'
``````

Sorting

``````things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road']

things.sort()  # alphanumeric order
print(things)
>>>'cat', 'cheese', 'dog', 'lemon', 'mouse', 'road'

things.sort(reverse=True)  # backwards
print(things)
>>>'road', 'mouse', 'lemon', 'dog', 'cheese', 'cat'
``````

Series based on