Python for Data Science — List Operations and Methods

List Methods

I covered creating, accessing, and slicing lists in my previous article. You can access the article here.

You haven’t gained an understanding of list methods yet, but it is essential to cover them now. You will be learning more about methods throughout this article and by the end, you will understand them quite well!

A Python list has several methods that you can call. Lists have default methods for manipulation with list elements.

Below are the methods you can use with a list:

  • append(): append() method helps to append a single object to the end of the list. The element added using the append() method will always be positioned at -1 or ( length-1) position in the list. append() method is only used when we want to add an element at the last index.
  • insert(): insert() method insert an element at the specified position where the append() method appends at the last index only.
  • extend() : extend() method appends the elements from iterable to given list. insert() and append() method used to add a single element at a time. But extend() method helps to append multiple elements at a time.
  • clear(): clear() is a list built-in method used to remove all the elements from the input list.
  • copy(): Occasionally you will want to copy a list. One simple way to copy your list is to use the copy method:
  • count() :count() method returns the number of elements with the specified value. Any type of object can be used as a parameter for the count method. count() method returns an integer number that shows the frequency of given elements if the element does not exist in a list it will return 0.
  • index(): index() method returns the first index of searched element otherwise returns an exception named ValueError if the value is not present in the list.
  • pop() : pop() method removes an element at a given index and returns that element. If the index is not provided the pop() method removes the last element from the list.
  • remove(): remove() method takes input as an element and removes that element from the list. It raises an exception if that element does not exist in the list.
  • reverse() :reverse() method returns an updated copy of the reversed list of the input list.

reverse() method applies to reverse the main list and modifies the indexes but when we need a temporary reverse of the list use reversed() python built-in function to return an object.

  • sort() :sort() method will return an ascending order sorted list. When the reverse parameter is given as True return descending order list.

I hope that you will find this article insightful. If you like to, please share the link with your friends, family, and colleagues.

Would you like me to encourage us to spread the insights on more topics, Please encourage us with a cup of coffee

For any suggestions/feedback please comment. Also please consider following and subscribing using my link.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store