Python Tuple tuple() Method

Python 元组Python Tuple


Description

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

Syntax

tuple() method syntax:

tuple( iterable )

Parameters

  • iterable -- the iterable sequence to convert to 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, returns a tuple composed 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 元组Python Tuple

Other Extensions