How Do You, in Python, Make a Tuple from a List?

Problem scenario
You are writing code in Python.  You have a list that you want to have a copy of in the form of a tuple.  What do you do to convert the content to a different data type?

Solution
Assuming you have a list called "contintlist", this line would create a tuple with the content of "contintlist":

cooltuple = tuple(contintlist)

Here is a Python program that generates a list, then creates a tuple with the same content:

import random
pretuplelist = []
for i in range(1000):
   pretuplelist.append(random.randint(1,100))
cool_tuple = tuple(pretuplelist)
print(cool_tuple)

Leave a comment

Your email address will not be published. Required fields are marked *