Python Tuple tuple() method
Description
The Python tuple tuple() function converts a list into a tuple.
Syntax
Syntax of the tuple() method:
tuple( iterable )
Parameters
- iterable -- an iterable sequence to be converted into a tuple.
Return Value
Returns a tuple.
Example
The following example demonstrates how to use the tuple() function:
Example 1
tuple
(
[
1
,
2
,
3
,
4
]
)
(
1
,
2
,
3
,
4
)
tuple
(
{
1
:
2
,
3
:
4
}
)
# For a dictionary, it returns a tuple made up of the dictionary's keys
(
1
,
3
)
tuple
(
(
1
,
2
,
3
,
4
)
)
# A tuple returns the tuple itself
(
1
,
2
,
3
,
4
)
Example 2
#!/usr/bin/python
aList
=
[
123
,
'
xyz
'
,
'
zara
'
,
'
abc
'
]
;
aTuple
=
tuple
(
aList
)
"
Tuple elements :
"
,
aTuple
The output of the above example is:
Tuple elements : (123, 'xyz', 'zara', 'abc')
Python Tuple