Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Tuple tuple() method

Python Tuple tuple() method

Python TuplePython Tuple


Description

The Python tuple tuple() function converts a list into a tuple.

Syntax

Syntax of the tuple() method:


tuple( iterable )

Parameters

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

)

print

"

Tuple elements :

"

,

aTuple

The output of the above example is:


Tuple elements :  (123, 'xyz', 'zara', 'abc')

Python TuplePython Tuple