Difference Between List & Series in Python
Hi! This short tutorial will explain the difference between lists and pandas series in the Python programming language.
The table of content is structured as follows:
Let’s get right into the discussion and code!
Python List
A Python list is used to store different data types, like strings, integers. Below are examples of Python lists containing integers, strings, a mix of objects in different data types, and user-defined objects from top to bottom:
# list of integers int_list = [9,8,7,6,5,4,3] print(int_list) # [9, 8, 7, 6, 5, 4, 3] # list of strings str_list = ["Japan", "Germany", "Brazil", "Nigeria", "Costa Rica"] print(str_list) # ['Japan', 'Germany', 'Brazil', 'Nigeria', 'Costa Rica'] # list with mixed data types mixed_list = [1,2,3,"dog","cat",[10.8,3.6,4.7]] print(mixed_list) # [1, 2, 3, 'dog', 'cat', [10.8, 3.6, 4.7]] # list of object class Groceries: def __init__(self, item, price): self.item = item self.price = price my_list = [] my_list.append(Groceries("Bread",25)) my_list.append(Groceries("Broccoli",15)) my_list.append(Groceries("Beef",30)) my_list.append(Groceries("Carrots",12)) my_list.append(Groceries("Butter",10)) # for loop to print object list for l in my_list: print(f"{l.item}({l.price})") # Bread(25) # Broccoli(15) # Beef(30) # Carrots(12) # Butter(10)
We can also create a Python list using the list() function:
# list of integers int_list = list((9,8,7,6,5,4,3)) print(int_list) # [9, 8, 7, 6, 5, 4, 3] # list of strings str_list = list(("Japan", "Germany", "Brazil", "Nigeria", "Costa Rica")) print(str_list) # ['Japan', 'Germany', 'Brazil', 'Nigeria', 'Costa Rica'] # list with mixed data types mixed_list = list((1,2,3,"dog","cat",[10.8,3.6,4.7])) print(mixed_list) # [1, 2, 3, 'dog', 'cat', [10.8, 3.6, 4.7]]
In the examples above, we parsed a tuple to the list constructor, which returned a list of integers, strings or some mixed of different data types.
Attributes of Python List
Below are the attributes of Python list:
- A list is an ordered collection of elements, where each element has a specific index starting from 0.
- Lists are mutable, meaning you can add, remove, or modify elements after creating the list.
- The elements in a list can be accessed by their index values.
- The indexes of a list are always integers.
- A list can hold duplicate values.
Python Series
Python pandas series is a one-dimensional array that can hold different data types, like integers, strings, and floats. The axis labels are collectively referred to as the index.
To create a pandas series, we will need to, first of all, install and import the pandas library:
# install pandas pip install pandas # import pandas import pandas as pd
With pandas installed and imported into our Python programming environment, we can now create examples of pandas series:
# series with default index sim_arr = [3,4,5,7,9] series1 = pd.Series(sim_arr) print(series1) #0 3 #1 4 #2 5 #3 7 #4 9 # series with defined index sim_arr = [3,4,5,7,9] series2 = pd.Series(sim_arr, index = [3,7,5,6,9]) print(series2) #3 3 #7 4 #5 5 #6 7 #9 9 # series of mixed data types mixed_arr = [5.2,4.8,2.1,"cat","dog",1,2,3] series3 = pd.Series(mixed_arr) print(series3) #0 5.2 #1 4.8 #2 2.1 #3 cat #4 dog #5 1 #6 2 #7 3
You can return any item in a series using its index number or position:
print(series3[3]) # 'cat'
Attributes of Python Series
Below are the attributes of pandas Series:
- Each element in the Series can be labeled with a unique index, which can be any hashable data type.
- The size of a Series can be changed dynamically by adding or removing elements.
- Pandas Series supports vectorized operations, meaning mathematical operations can be performed on an entire Series.
- When performing operations between two Series, pandas aligns the data based on their labels.
- Pandas Series can gracefully handle missing or NaN (Not a Number) values.
- A Series can be sliced like a NumPy array using labels or integer positions.
- A scalar value can be broadcast to all the elements in the Series.
- Pandas Series can be created from a variety of data sources, including NumPy arrays, lists, and dictionaries.
So while pandas series may have some inherent differences from the Python list, they can be used together in a powerful way to efficiently manipulate and analyze one-dimensional data with flexible and labeled indexing capabilities.
We have examined the difference between lists and pandas series in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on the difference between lists and pandas series in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail the difference between lists and pandas series in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Difference between List & Set in Python
- count() Method for Lists in Python (2 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Change Index of Element in List in Python(Example)
- Learn Python Programming
This post has shown the difference between lists and pandas series in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter