In this python tuple program, we will create a list of tuples from a list having a number and its square in each tuple.
Steps to solve the program
- Take a list as input.
- Use the list comprehension method to create a list of tuples from a list having a number and its square in each tuple.
- Use for loop to iterate over elements in the list and use pow() to get the square of the elements.
- Perform the above operations inside a list to achieve list comprehension.
- Print the output.
list1 = [4,6,3,8]
tup = [(val, pow(val, 2)) for val in list1]
print(tup)
Output :
[(4, 16), (6, 36), (3, 9), (8, 64)]
1 thought on “Create a list of tuples from a list having a number and its square in each tuple”