convert to tuple python
Python hosting: Host, run, and code Python in the cloud!
The tuple data structure is used to store a group of data. The elements in this group are separated by a comma. Once created, the values of a tuple cannot change.
An example of a tuple would be:
|
Related course:
Python Programming Bootcamp: Go from zero to hero
Convert list to a tuple
To convert a list to a tuple you can use the tuple() function.
#!/usr/bin/env python |
Convert tuple to list
You can convert an existing tuple to a list using the list() function:
#!/usr/bin/env python |
Convert tuple to string
If your tuple contains only strings (text) you can use:#!/usr/bin/env python |
Sort a tuple
Tuples are arrays you cannot modify and don’t have any sort function.
But, there is a way. You can however use the sorted() function which returns a list. This list can be converted to a new tuple.
#!/usr/bin/env python |
Keep in mind a tuple cannot be modified, we simple create a new tuple that happens to be sorted.
Posted in Documentation
Leave a Reply: