Lists in Python
Lists are the most significant and flexible sequence types in Python. A list in Python is simply an ordered collection of items each of which can be of any type. A list is a dynamic mutable data structure which means that items can be added to and deleted from it.
List constants are surrounded by square brackets and the elements in the list are separated by commas. The individual elements of the list can be accessed using the square brackets by specifying the item's index. The first element is at index 0. Indexes range from 0 to n-1 (where n is the length of the list) and from -1 to -n.
Lists are heterogeneous, i.e., values can be of any type (strings are homogeneous because their elements are characters).
The following is an example of a list and how we can access elements of the list.
>>> vals = [1, 2, 3, 4, 5, 6] # Define a new list
>>> vals # Print List
[1, 2, 3, 4, 5, 6]
>>> vals[0] # Print first item in list
1
>>> vals[3] # print fourth item in list
4
>>> i = 2 # Create a Variable
>>> vals[2] # Pass the index value using the variable
3
>>>
An index value of −1 is used to reference the last item of a list and an index value of −2 is used to reference the previous to last item of the list. Examples below:
>>> vals[-1] # Access last item of the list
6
>>> vals[-2] # Access second last item of the list
5
>>>
Lists are Mutable
Lists are mutable, which means that the items of the list can change value by performing assignment on them. In the following example, we assign a new value of 12 to item at index 3.
>>> vals
[1, 2, 3, 4, 5, 6]
>>> vals[3] = 12 # Assign new value to item at index 3
>>> vals # Notice the change in value of vals[3]
[1, 2, 3, 12, 5, 6]
>>>
Slicing Operations
The slicing operations are used to access a sublist of the list. The colon notation is used to specify the range of index values of the items. The first index value is written before the colon and the last index value is written after the colon. This indicates the range of index values from the start index value (inclusive) up to but not including the last index value (exclusive) specified.
In the following example, we first define a list called mylist
which contains 6 elements. The second command specifies a sublist of list mylist
, that includes the items starting with index value 0 up to but not including the item with index value 4. The third command assigns the sublist mylist[2:5] to variable mysublist
.
>>> mylist = [1,2,3,4,5,6]
>>> mylist[0:4]
[1, 2, 3, 4]
>>> mysublist = mylist[2:5]
>>> mysublist
[3, 4, 5]
In a list, we can update a range of items using slicing and assignment. For example, the following command changes the values of items with index 0 and up to but not including the item with index value 2 with another set of values.
>>> mylist[0:2]=mylist[1:3]
>>> mylist
[2, 3, 3, 4, 5, 6]
Using slicing, we can leave the second index value which implies that the range will be upto the last item. Similarly, we can leave the first index value which implies that the range of items starts with the first item of the list.
>>> mylist[2:]
[3, 4, 5, 6]
>>> mylist[:4]
[2, 3, 3, 4]
Sometimes you may need to count the number of items in a list. This can be done using the len()
method as shown below:
>>> len('mylist')
6
>>>
Just like the len()
method, all Python objects have many methods attached to them which allow us to perform various actions on the objects. We will learn about these methods in the coming lessons.
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $29 (Regular $57)Free Guides - Getting Started with R and Python
Enter your name and email address below and we will email you the guides for R programming and Python.