Python Lists and List Manipulation
In Python, lists store an ordered collection of items which can be of different types. This post is an overview of lists and their manipulation.
By Michael Galarnyk, Data Scientist
Defining a List
Lists are written within square brackets 
Lists store an ordered collection of items which can be of different types. The list defined above has items that are all of the same type (int), but all the items of a list do not need to be of the same type as you can see below.
The list contains an int, a bool, a string, and a float.
Access Values in a List
Each item in a list has an assigned index value. It is important to note that python is a zero indexed based language. All this means is that the first item in the list is at index 0.
Python also supports negative indexing. Negative indexing starts from the end. It can be more convenient at times to use negative indexing to get the last item in the list because you don’t have to know the length of the list to access the last item.
As a reminder, you could also access the same item using positive indexes (as seen below).
Slice of Lists
Slices are good for getting a subset of values in your list. For the example code below, it will return a list with the items from index 0 up to and not including index 2.
The code below returns a list with items from index 1 to the end of the list
Update Item in a List
Lists in Python are mutable. All that means is that after defining a list, it is possible to update the individual items in a list.
Python lists have different methods that help you modify a list. This section of the tutorial just goes over various python list methods.
The index method returns the first index at which a value occurs. In the code below, it will return 0.
You can also specify where you start your search.
The count method works just like how it sounds. It counts the number of times a value occurs in a list
The sort method sorts and alters the original list in place.
The code above sorts a list from low to high. The code below shows that you can also sort a list from high to low.
As an aside, I should mention that you can also sort a list of strings from a-z and z-a as you can see here.
The append method adds an element to the end of a list. This happens inplace.
The remove method removes the first occurrence of a value in a list.
The pop method removes an item at the index you provide. This method will also return the item you removed from the list. If you don’t provide an index, it will by default remove the item at the last index.
The method extends a list by appending items. The benefit of this is you can add lists together.
Alternatively, the same thing could be accomplished by using the + operator.
The insert method inserts an item before the index you provide
Please let me know if you have any questions either here or in the comments section of the youtube video or through Twitter! Next post reviews python dictionaries. If you want to learn how to utilize the Pandas, Matplotlib, or Seaborn libraries, please consider taking my Python for Data Visualization LinkedIn Learning course. Here is a free preview video.
Bio: Michael Galarnyk is a Data Scientist and Corporate Trainer. He currently works at Scripps Translational Research Institute. You can find him on Twitter (https://twitter.com/GalarnykMichael), Medium (https://medium.com/@GalarnykMichael), and GitHub (https://github.com/mGalarnyk).
Original. Reposted with permission.
- Understanding Boxplots
- Set Operations Applied to Pandas DataFrames
- Understanding Decision Trees for Classification in Python
Top Stories Past 30 Days