
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create List of Tuples with Number and Its Cube Using Python
When it is required to create a list from a given list that have a number and its cube, the list comprehension can be used.
Below is the demonstration of the same −
Example
my_list = [32, 54, 47, 89] print("The list is ") print(my_list) my_result = [(val, pow(val, 3)) for val in my_list] print("The result is ") print(my_result)
Output
The list is [32, 54, 47, 89] The result is [(32, 32768), (54, 157464), (47, 103823), (89, 704969)]
Explanation
A list is defined, and is displayed on the console.
The list comprehension is used to iterate through the list, and use the ‘pow’ method to get the cube of the integer.
This is assigned to a result.
This result is displayed as output on the console.
Advertisements