Getting started with Python LanguageList comprehensionsFilterListFunctionsDecoratorsMath ModuleLoopsRandom moduleComparisonsImporting modulesSorting, Minimum and MaximumOperator moduleVariable Scope and BindingBasic Input and OutputFiles & Folders I/OJSON ModuleString MethodsMetaclassesIndexing and SlicingGeneratorsSimple Mathematical OperatorsReduceMap FunctionExponentiationSearchingDictionaryClassesCountingManipulating XMLDate and TimeSetCollections moduleParallel computationMultithreadingWriting extensionsUnit TestingRegular Expressions (Regex)Bitwise OperatorsIncompatibilities moving from Python 2 to Python 3Virtual environmentsCopying dataTupleContext Managers (“with” Statement)Hidden FeaturesEnumString FormattingConditionalsComplex mathUnicode and bytesThe __name__ special variableChecking Path Existence and PermissionsPython NetworkingAsyncio ModuleThe Print Functionos.pathCreating Python packagesParsing Command Line argumentsHTML ParsingSubprocess Librarysetup.pyList slicing (selecting parts of lists)SocketsItertools ModuleRecursionBoolean OperatorsThe dis moduleType Hintspip: PyPI Package ManagerThe locale ModuleExceptionsWeb scraping with PythonDeque ModuleDistributionProperty ObjectsOverloadingDebuggingReading and Writing CSVDynamic code execution with `exec` and `eval`PyInstaller - Distributing Python CodeIterables and IteratorsData Visualization with PythonThe Interpreter (Command Line Console)*args and **kwargsFunctools ModuleGarbage CollectionIndentationSecurity and CryptographyPickle data serialisationurllibBinary DataPython and ExcelIdiomsMethod OverridingDifference between Module and PackageData SerializationPython concurrencyIntroduction to RabbitMQ using AMQPStormPostgreSQLDescriptorCommon PitfallsMultiprocessingtempfile NamedTemporaryFileWorking with ZIP archivesStackProfilingUser-Defined MethodsWorking around the Global Interpreter Lock (GIL)DeploymentLoggingProcesses and ThreadsThe os ModuleComments and DocumentationDatabase AccessPython HTTP ServerAlternatives to switch statement from other languagesList destructuring (aka packing and unpacking)Accessing Python source code and bytecodeMixinsAttribute AccessArcPyPython Anti-PatternsPlugin and Extension ClassesWebsocketsImmutable datatypes(int, float, str, tuple and frozensets)String representations of class instances: __str__ and __repr__ methodsArraysOperator PrecedencePolymorphismNon-official Python implementationsList ComprehensionsWeb Server Gateway Interface (WSGI)2to3 toolAbstract syntax treeAbstract Base Classes (abc)UnicodeSecure Shell Connection in PythonPython Serial Communication (pyserial)Neo4j and Cypher using Py2NeoBasic Curses with PythonPerformance optimizationTemplates in pythonPillowThe pass statementLinked List Nodepy.testDate FormattingHeapqtkinterCLI subcommands with precise help outputDefining functions with list argumentsSqlite3 ModulePython PersistenceTurtle GraphicsConnecting Python to SQL ServerDesign PatternsMultidimensional arraysAudioPygletQueue ModuleijsonWebbrowser ModuleThe base64 ModuleFlaskgroupby()Sockets And Message Encryption/Decryption Between Client and ServerpygameInput, Subset and Output External Data Files using Pandashashlibgetting start with GZipDjangoctypesCreating a Windows service using PythonPython Server Sent EventsMutable vs Immutable (and Hashable) in PythonPython speed of programconfigparserLinked listsCommonwealth ExceptionsOptical Character RecognitionPython Data TypesPartial functionspyautogui modulegraph-toolUnzipping FilesFunctional Programming in PythonPython Virtual Environment - virtualenvsysvirtual environment with virtualenvwrapperCreate virtual environment with virtualenvwrapper in windowsPython Requests PostPlotting with MatplotlibPython Lex-YaccChemPy - python packagepyaudioshelveUsage of "pip" module: PyPI Package ManagerIoT Programming with Python and Raspberry PICode blocks, execution frames, and namespaceskivy - Cross-platform Python Framework for NUI DevelopmentCall Python from C#Similarities in syntax, Differences in meaning: Python vs. JavaScriptWriting to CSV from String or ListRaise Custom Errors / ExceptionsUsing loops within functionsPandas Transform: Preform operations on groups and concatenate the results

Tuple

Other topics

Remarks:

Parentheses are only needed for empty tuples or when used in a function call.

A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable and are hashable, so they can be used in sets and maps

Indexing Tuples

x = (1, 2, 3)
x[0]  # 1
x[1]  # 2
x[2]  # 3
x[3]  # IndexError: tuple index out of range

Indexing with negative numbers will start from the last element as -1:

x[-1] # 3
x[-2] # 2
x[-3] # 1
x[-4] # IndexError: tuple index out of range

Indexing a range of elements

print(x[:-1])   # (1, 2)
print(x[-1:])   # (3,)
print(x[1:3])   # (2, 3)

Tuples are immutable

One of the main differences between lists and tuples in Python is that tuples are immutable, that is, one cannot add or modify items once the tuple is initialized. For example:

>>> t = (1, 4, 9)
>>> t[0] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

Similarly, tuples don't have .append and .extend methods as list does. Using += is possible, but it changes the binding of the variable, and not the tuple itself:

>>> t = (1, 2)
>>> q = t
>>> t += (3, 4)
>>> t
(1, 2, 3, 4)
>>> q
(1, 2)

Be careful when placing mutable objects, such as lists, inside tuples. This may lead to very confusing outcomes when changing them. For example:

>>> t = (1, 2, 3, [1, 2, 3])
(1, 2, 3, [1, 2, 3])
>>> t[3] += [4, 5]

Will both raise an error and change the contents of the list within the tuple:

TypeError: 'tuple' object does not support item assignment
>>> t
(1, 2, 3, [1, 2, 3, 4, 5])

You can use the += operator to "append" to a tuple - this works by creating a new tuple with the new element you "appended" and assign it to its current variable; the old tuple is not changed, but replaced!

This avoids converting to and from a list, but this is slow and is a bad practice, especially if you're going to append multiple times.

Tuple Are Element-wise Hashable and Equatable

hash( (1, 2) )  # ok
hash( ([], {"hello"})  # not ok, since lists and sets are not hashabe

Thus a tuple can be put inside a set or as a key in a dict only if each of its elements can.

{ (1, 2) } #  ok
{ ([], {"hello"}) ) # not ok

Tuple

Syntactically, a tuple is a comma-separated list of values:

t = 'a', 'b', 'c', 'd', 'e'

Although not necessary, it is common to enclose tuples in parentheses:

t = ('a', 'b', 'c', 'd', 'e')

Create an empty tuple with parentheses:

t0 = ()
type(t0)            # <type 'tuple'>

To create a tuple with a single element, you have to include a final comma:

t1 = 'a',
type(t1)              # <type 'tuple'>

Note that a single value in parentheses is not a tuple:

t2 = ('a')
type(t2)              # <type 'str'>

To create a singleton tuple it is necessary to have a trailing comma.

t2 = ('a',)
type(t2)              # <type 'tuple'>

Note that for singleton tuples it's recommended (see PEP8 on trailing commas) to use parentheses. Also, no white space after the trailing comma (see PEP8 on whitespaces)

t2 = ('a',)           # PEP8-compliant
t2 = 'a',             # this notation is not recommended by PEP8
t2 = ('a', )          # this notation is not recommended by PEP8

Another way to create a tuple is the built-in function tuple.

t = tuple('lupins')
print(t)              # ('l', 'u', 'p', 'i', 'n', 's')
t = tuple(range(3))
print(t)              # (0, 1, 2)

These examples are based on material from the book Think Python by Allen B. Downey.

Packing and Unpacking Tuples

Tuples in Python are values separated by commas. Enclosing parentheses for inputting tuples are optional, so the two assignments

a = 1, 2, 3   # a is the tuple (1, 2, 3)

and

a = (1, 2, 3) # a is the tuple (1, 2, 3)

are equivalent. The assignment a = 1, 2, 3 is also called packing because it packs values together in a tuple.

Note that a one-value tuple is also a tuple. To tell Python that a variable is a tuple and not a single value you can use a trailing comma

a = 1  # a is the value 1
a = 1, # a is the tuple (1,)

A comma is needed also if you use parentheses

a = (1,) # a is the tuple (1,)
a = (1)  # a is the value 1 and not a tuple

To unpack values from a tuple and do multiple assignments use

# unpacking AKA multiple assignment
x, y, z = (1, 2, 3) 
# x == 1
# y == 2
# z == 3

The symbol _ can be used as a disposable variable name if one only needs some elements of a tuple, acting as a placeholder:

a = 1, 2, 3, 4
_, x, y, _ = a
# x == 2
# y == 3

Single element tuples:

x, = 1,  # x is the value 1
x  = 1,  # x is the tuple (1,)

In Python 3 a target variable with a * prefix can be used as a catch-all variable (see Unpacking Iterables ):

Python 3.x3.0
first, *more, last = (1, 2, 3, 4, 5)
# first == 1
# more == [2, 3, 4]
# last == 5

Reversing Elements

Reverse elements within a tuple

colors = "red", "green", "blue"
rev = colors[::-1]
# rev: ("blue", "green", "red")
colors = rev
# colors: ("blue", "green", "red")

Or using reversed (reversed gives an iterable which is converted to a tuple):

rev = tuple(reversed(colors))
# rev: ("blue", "green", "red")
colors = rev
# colors: ("blue", "green", "red")

Built-in Tuple Functions

Tuples support the following build-in functions

Comparison

If elements are of the same type, python performs the comparison and returns the result. If elements are different types, it checks whether they are numbers.

  • If numbers, perform comparison.
  • If either element is a number, then the other element is returned.
  • Otherwise, types are sorted alphabetically .

If we reached the end of one of the lists, the longer list is "larger." If both list are same it returns 0.

tuple1 = ('a', 'b', 'c', 'd', 'e')
tuple2 = ('1','2','3')
tuple3 = ('a', 'b', 'c', 'd', 'e')

cmp(tuple1, tuple2)
Out: 1

cmp(tuple2, tuple1)
Out: -1

cmp(tuple1, tuple3)
Out: 0

Tuple Length

The function len returns the total length of the tuple

len(tuple1)
Out: 5

Max of a tuple

The function max returns item from the tuple with the max value

max(tuple1)
Out: 'e'

max(tuple2)
Out: '3'

Min of a tuple

The function min returns the item from the tuple with the min value

min(tuple1)
Out: 'a'

min(tuple2)
Out: '1'

Convert a list into tuple

The built-in function tuple converts a list into a tuple.

list = [1,2,3,4,5]
tuple(list)
Out: (1, 2, 3, 4, 5)

Tuple concatenation

Use + to concatenate two tuples

tuple1 + tuple2
Out: ('a', 'b', 'c', 'd', 'e', '1', '2', '3')

Syntax:

  • (1, a, "hello") # a must be a variable

  • () # an empty tuple

  • (1,) # a 1-element tuple. (1) is not a tuple.

  • 1, 2, 3 # the 3-element tuple (1, 2, 3)

Contributors

Topic Id: 927

Example Ids: 3062,3077,5104,6599,8400,11740,17570

This site is not affiliated with any of the contributors.