DEV Community

Vicki Langer
Vicki Langer

Posted on • Edited 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 = [] 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Changing Lists

Lists are mutable, or modifiable.

Add via Index

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'] 
Enter fullscreen mode Exit fullscreen mode

Add to end

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'] 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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 >>> 
Enter fullscreen mode Exit fullscreen mode

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 print(newer_list) >>>"table", "book", "blanket", 'cat', 'dog', 'mouse', 'cheese', 'lemon', 'road' 
Enter fullscreen mode Exit fullscreen mode

Counting

things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road'] print(things.count()) >>>6 
Enter fullscreen mode Exit fullscreen mode

Reversing

things = ['cat', 'dog', 'mouse', 'cheese', 'lemon', 'road'] things.reverse() # flip the order of the list print(things) >>>'road', 'lemon', 'cheese', 'mouse', 'dog', 'cat' 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

Series based on

Top comments (0)