The most important collection data type in Python is the
list data type. You’ll use lists basically in all your future projects so take 3-5 minutes and study this short guide carefully.
You can also play my short video tutorial as you read over the methods:
|Appends element |
|Removes all elements from the list |
|Returns a copy of the list |
|Counts the number of occurrences of element |
|Adds all elements of an iterable |
|Returns the position (index) of the first occurrence of value |
|Inserts element |
|Removes and returns the final element of the list |
|Removes and returns the first occurrence of element |
|Reverses the order of elements in the list |
|Sorts the elements in the list |
Go ahead and try the Python list methods yourself:
Puzzle: Can you figure out all outputs of this interactive Python script?
If you’ve studied the table carefully, you’ll know the most important list methods in Python. Let’s have a look at some examples of above methods:
>>> l =  >>> l.append(2) >>> l  >>> l.clear() >>> l  >>> l.append(2) >>> l  >>> l.copy()  >>> l.count(2) 1 >>> l.extend([2,3,4]) >>> l [2, 2, 3, 4] >>> l.index(3) 2 >>> l.insert(2, 99) >>> l [2, 2, 99, 3, 4] >>> l.pop() 4 >>> l.remove(2) >>> l [2, 99, 3] >>> l.reverse() >>> l [3, 99, 2] >>> l.sort() >>> l [2, 3, 99]
- Have a look at the ultimate guide of Python lists.
- Master Python Sets (with Harry Potter Examples)
- Master Python Dictionaries (the ultimate blog tutorial)
- Join my free email computer science academy for continuous improvement in Python
Here’s your free PDF cheat sheet showing you all Python list methods on one simple page. Click the image to download the high-resolution PDF file, print it, and post it to your office wall: