PiGuy PiGuy - 1 month ago 10
Python Question

What is the purpose of putting a list in a tuple?

I hope your having a wonderful day! I was watching some online tutorials and found out you can put a list in a tuple. I am still trying to wrap my head around the use of this. Where and why would it be useful. Can you explain?

mylist = [1,2,3,4,5]
mytup = (mylist)

print(mytup)
tuplist = input('Is it useful putting a list in a tuple? Y/N')
if tuplist == 'Y':
print('Why?')
if tuplist == 'N':
print('Why not?')

Answer

Since this is what you seem to be looking for, I guess I can post an example as answer.

Imagine you have a function returning a list, but you also want to know if something has occurred. You could do something like this:

def my_function(myList) :
    flag = True
    #a lot of processing, where flag becomes False if something bad happens
    return (myProcessedList, flag)

Note however that there would be many better ways to do so, but something like this might become useful in some situations.