- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number
When it is required to create a list of tuple, and have the first element as the number, and the second element as the square of the element, list comprehension can be used.
Below is the demonstration of the same −
my_list = [23, 42, 67, 89, 11, 32] print(“The list is “) print(my_list) print(“The resultant tuple is :”) my_result = [(val, pow(val, 2)) for val in my_list] print(my_result)
The list is [23, 42, 67, 89, 11, 32] The resultant tuple is : [(23, 529), (42, 1764), (67, 4489), (89, 7921), (11, 121), (32, 1024)]
A list is defined, and is displayed on the console.
The list comprehension is used and the ‘pow’ method is used to find the square of the number.
This is converted to a list.
The output is displayed on the console.
- Remove Tuples from the List having every element as None in Python
- Find the tuples containing the given element from a list of tuples in Python
- Python – Append given number with every element of the list
- Python program to create a list of tuples from the given list having the number and its cube in each tuple
- Python program to sort a list according to the second element in the sublist.
- Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion
- How to get the second-to-last element of a list in Python?
- Python program to find the second largest number in a list
- Python – Remove Tuples from a List having every element as None
- Get first element with maximum value in list of tuples in Python
- Python program to sort a list according to the second element in sublist
- Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion
- How to get the first element of the List in Java?
- Python | Sort the values of first list using second list