How to Split a List of Tuples into Two Separate Lists in Python – 4 Easy Methods
This article shows how to transform a list of (letter, number) tuples into two independent lists using four different Python approaches—including basic loops, zip unpacking, pandas, and numpy—complete with code examples and output screenshots.
Hello, I’m a Python enthusiast. Yesterday a fan asked how to split a list of tuples into two separate lists.
Original list:
[("a", 1), ("a", 2), ("a", 3), ("b", 1), ("b", 2), ("b", 3), ("c", 1), ("c", 2), ("c", 3)]Goal: separate the letters and numbers into two lists.
Implementation Process
Method One – Simple Loop
Use a straightforward loop to append each element to its own list.
# coding:utf-8
# @Time : 2022/5/6 11:46
# @Author: PiPi
letter_list = []
num_list = []
list1 = [("a", 1), ("a", 2), ("a", 3), ("b", 1), ("b", 2), ("b", 3), ("c", 1), ("c", 2), ("c", 3)]
for i in list1:
letter_list.append(i[0])
num_list.append(i[1])
print(letter_list)
print(num_list)Method Two – Zip Unpacking
Leverage zip to unpack the list in one line.
list1 = [("a", 1), ("a", 2), ("a", 3), ("b", 1), ("b", 2), ("b", 3), ("c", 1), ("c", 2), ("c", 3)]
list_result = tuple(zip(*list1))
letter_list = list(list_result[0])
num_list = list(list_result[1])
print(letter_list)
print(num_list)A more concise form:
letter_list, num_list = zip(*list1)
print(letter_list)
print(num_list)Method Three – Using pandas
Convert the list to a DataFrame and extract columns.
import pandas as pd
list1 = [("a", 1), ("a", 2), ("a", 3), ("b", 1), ("b", 2), ("b", 3), ("c", 1), ("c", 2), ("c", 3)]
df = pd.DataFrame(list1)
print(df[0].tolist())
print(df[1].tolist())Method Four – Using numpy
Transform the list to a NumPy array and transpose.
import numpy as np
list1 = [("a", 1), ("a", 2), ("a", 3), ("b", 1), ("b", 2), ("b", 3), ("c", 1), ("c", 2), ("c", 3)]
letter_list, num_list = np.array(list1).T.tolist()
print(letter_list)
print(num_list)Summary
We presented four ways to split a list of tuples into two separate lists in Python. All methods achieve the same result, and you can choose the one that best fits your workflow or library preferences.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
