Python List Methods

Just like string methods, list methods are working on the list it’s being called from, if you for example have a list called: list1 = [“Movies”, “Music”, “Pictures”], then the list method is called such this: list1.list_method() Let’s demonstrate this by typing in some list method into the python interpreter.

>>> list1 = ["Movies", "Music", "Pictures"]

#list1.append(x) will add an element to the end of the list
>>> list1.append("Files")
>>> list1
['Movies', 'Music', 'Pictures', 'Files']

#list1.insert(x, y) will add element y on the place before x
>>> list1.insert(2,"Documents")
>>> list1
['Movies', 'Music', 'Documents', 'Pictures', 'Files']

#list1.remove(x) will remove the element x from the list
>>> list1.remove("Files")
>>> list1
['Movies', 'Music', 'Documents', 'Pictures']

#list1.extend(x) will join the list with list x
>>> list2 = ["Music2", "Movies2"]
>>> list1.extend(list2)
>>> list1
['Movies', 'Music', 'Documents', 'Pictures', 'Music2', 'Movies2']

Please take a look at this List Methods post, which describes more List methods

Leave a Reply

Your email address will not be published.