Oct. 04, 2012

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 on this post, which describes more List methods

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.

 



Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.