How to Create a List of Tuples from Elements in Python


In this example we will show how to create a list of tuples, where the first element is a number and the second element is the square of the first element.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

lowerLimit = int(input("Please input the lower limit: "))
upperLimit = int(input("Please input the upper limit: "))
n = [(i, i * i) for i in range(lowerLimit, upperLimit + 1)]
print("The list is:", n)

Output:

Please input the lower limit: 3
Please input the upper limit: 6
The list is: [(3, 9), (4, 16), (5, 25), (6, 36)]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments