- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Create a List of Tuples in Python?
In Python, creating a list of tuples is a flexible data structure for storing many items. Tuples are immutable, ordered collections that are suitable for grouping similar items. This tutorial will guide you through this with several examples to build and handle lists of tuples.
Understanding Tuples
Tuples are ordered collections of elements. Lists are mutable collections of elements enclosed in square brackets, that combine with tuples to form a list of tuples. List comprehension gives compact code. It also unpacks and assigns variables to each member necessary for quick access. Nesting tuples within lists combines tuples and lists is done for structured data representation.
Example
# Create a list of tuples containing student records student_records = [ ("Atharva Shah", 25, "A"), ("Student 2", 21, "B"), ("Bob Ross", 23, "A"), ] # Iterate through the list and display each student's details for record in student_records: name, age, grade = record print(f"Name: {name}, Age: {age}, Grade: {grade}")
Create a list of student_records with three tuples representing student details: name, age, and grade. Iterate through the list, unpack each tuple, and print the details using f-string formatting.
Example 2: Using List Comprehension
numbers = [1, 2, 3, 4, 5] squares = [num ** 2 for num in numbers] cubes = [num ** 3 for num in numbers] # Combine lists into a list of tuples result = list(zip(numbers, squares, cubes)) # Display the list of tuples print(result) # [(1, 1, 1), (2, 4, 8), (3, 9, 27), (4, 16, 64), (5, 25, 125)]
Start with two lists of integers, squares, and cubes. Apply list comprehension to compute the square and cube of each number. Combine the lists element by element into a list of tuples, generating a new tuple for each set. Using the list function, convert the zip object to a list. Output the tuple list that includes the original integer, square, and cube. Then, make a list of student records that has three tuples reflecting student information: name, age, and grade. Iterate through the list, unpacking each tuple and printing the information in f-string format.
Alternatives
Loop to append tuples to an empty list
data = [] for i in range(1, 6): data.append((i, i ** 2)) #[(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
Map - Apply a function to each element in a list
numbers = [4, 5] squares = list(map(lambda x: (x, x ** 2), numbers)) # [(4, 16), (5, 25)]
Combine two lists into a list of tuples with zip()
names = ["Alice", "Bob", "Charlie"] ages = [25, 30, 22] person_info = list(zip(names, ages)) # [('Alice', 25), ('Bob', 30), ('Charlie', 22)]
Dynamic values with comprehension
even = [num for num in range(2, 11, 4)] tuples_with_squares = [(num, num ** 2) for num in even] # [(2, 4), (6, 36), (10, 100)]
Applications
Lists of tuples are used to store and manipulate complex information.
Each tuple corresponds to a row in a database table. Efficient data grouping and management.
Applied in game development to store information. (Characters, scores, and achievements, for tracking, comparison, and unlocking new levels/prizes).
Simplified extraction of information.
Linguistic analysis in natural language processing (NLP).
They are the basic data structure for arranging and manipulating complicated data.
Conclusion
This guide was all about making lists of tuples in Python for manipulating data. Indexing and slicing are techniques, as are loops for iterating over items. We have also discussed examples of real-world applications (DBMS and scientific data processing). Understanding these principles provides you with the tools to manage complicated data structures and improve your programming abilities.