Converting a pandas Series to a Python list is a common operation when you need to pass data to functions that expect standard Python types, serialize data for APIs, or simply work with list-based operations. The tolist() method provides a clean and efficient way to make this conversion.
In this guide, you'll learn how to convert a pandas Series to a list using tolist(), handle different data types, and understand when this conversion is useful.
Using tolist() to Convert a Series
The tolist() method converts a pandas Series into a standard Python list. It works with any data type - integers, floats, strings, or mixed types.
Converting a Numeric Series
import pandas as pd
numbers = pd.Series([2, 4, 6, 8, 10])
print("Pandas Series:")
print(numbers)
print(f"Type: {type(numbers)}")
numbers_list = numbers.tolist()
print("\nPython list:")
print(numbers_list)
print(f"Type: {type(numbers_list)}")Output:
Pandas Series:
0 2
1 4
2 6
3 8
4 10
dtype: int64
Type: <class 'pandas.core.series.Series'>
Python list:
[2, 4, 6, 8, 10]
Type: <class 'list'>Converting a String Series
import pandas as pd
import numpy as np
animals = pd.Series(
np.array(['ant', 'bat', 'cat', 'dog']),
index=[100, 101, 102, 103]
)
print("Pandas Series:")
print(animals)
animal_list = animals.tolist()
print(f"\nPython list: {animal_list}")
print(f"Type: {type(animal_list)}")Output:
Pandas Series:
100 ant
101 bat
102 cat
103 dog
dtype: object
Python list: ['ant', 'bat', 'cat', 'dog']
Type: <class 'list'>Converting a Series Created from a Dictionary
import pandas as pd
marks = {'Maths': 100.0, 'Physics': 90.0, 'Chemistry': 85.0}
series = pd.Series(marks)
print("Pandas Series:")
print(series)
marks_list = series.tolist()
print(f"\nValues as list: {marks_list}")
print(f"Index as list: {series.index.tolist()}")Output:
Pandas Series:
Maths 100.0
Physics 90.0
Chemistry 85.0
dtype: float64
Values as list: [100.0, 90.0, 85.0]
Index as list: ['Maths', 'Physics', 'Chemistry']Converting a Slice of a Series
You can slice a Series first and then convert the subset to a list:
import pandas as pd
scores = pd.Series(
[100, 90, 80, 90, 85],
index=['Student1', 'Student2', 'Student3', 'Student4', 'Student5']
)
top_three = scores[:3]
print("First 3 students:")
print(top_three)
print(f"\nAs list: {top_three.tolist()}")
print(f"Type: {type(top_three.tolist())}")Output:
First 3 students:
Student1 100
Student2 90
Student3 80
dtype: int64
As list: [100, 90, 80]
Type: <class 'list'>Alternative: Using list() Constructor
You can also use Python's built-in list() function to convert a Series:
import pandas as pd
series = pd.Series([10, 20, 30, 40])
result1 = series.tolist()
result2 = list(series)
print(f"tolist(): {result1}")
print(f"list(): {result2}")
print(f"Equal: {result1 == result2}")Output:
tolist(): [10, 20, 30, 40]
list(): [10, 20, 30, 40]
Equal: TrueHere is the corrected version of your section:
Handling Missing Values (NaN)
When a Series contains NaN values, tolist() converts them to Python's float('nan') or None depending on the dtype:
import pandas as pd
import numpy as np
numeric = pd.Series([1.0, np.nan, 3.0, np.nan, 5.0])
print(f"Numeric with NaN: {numeric.tolist()}")
strings = pd.Series(['apple', None, 'cherry'])
print(f"Strings with None: {strings.tolist()}")Output:
Numeric with NaN: [1.0, nan, 3.0, nan, 5.0]
Strings with None: ['apple', None, 'cherry']To replace NaN values before converting, use fillna():
import pandas as pd
import numpy as np
series = pd.Series([1.0, np.nan, 3.0, np.nan, 5.0])
clean_list = series.fillna(0).tolist()
print(f"Cleaned list: {clean_list}")Output:
Cleaned list: [1.0, 0.0, 3.0, 0.0, 5.0]Converting DataFrame Columns to Lists
Since each DataFrame column is a Series, tolist() works on columns too:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Score': [88.5, 92.3, 76.8]
})
names = df['Name'].tolist()
ages = df['Age'].tolist()
print(f"Names: {names}")
print(f"Ages: {ages}")Output:
Names: ['Alice', 'Bob', 'Charlie']
Ages: [25, 30, 35]Practical Example: Preparing Data for JSON
A common use case for tolist() is preparing pandas data for JSON serialization, since JSON doesn't support NumPy types:
import pandas as pd
import json
series = pd.Series([100, 200, 300], index=['a', 'b', 'c'])
data = {
'values': series.tolist(),
'labels': series.index.tolist()
}
json_string = json.dumps(data)
print(json_string)Output:
{"values": [100, 200, 300], "labels": ["a", "b", "c"]}Summary
| Method | Returns Native Python Types* | Preserves Index |
|---|---|---|
series.tolist() |
✅ Yes | ❌ No |
list(series) |
✅ Yes (for standard dtypes) | ❌ No |
series.to_dict() |
✅ Yes | ✅ Yes (as dict keys) |
series.values.tolist() |
✅ Yes | ❌ No |
Conclusion
Converting a pandas Series to a Python list is simple and versatile:
- Use
series.tolist()for the cleanest conversion - it returns a standard Python list with native Python types, making it ideal for JSON serialization, API calls, and interoperability with non-pandas code. - Use
list(series)when NumPy types are acceptable and you want a quick conversion. - Use
series.index.tolist()when you also need the index values as a list. - Handle
NaNvalues withfillna()before converting if your downstream code doesn't support missing values.
For most use cases, tolist() is the recommended and most reliable method.