0% found this document useful (0 votes)
12 views2 pages

Nested Tuples and Lists in Python

The document explains the concept of nested tuples and lists in Python, detailing how to define one tuple inside another and the syntax involved. It provides examples demonstrating the indexing and slicing operations on inner tuples and lists, as well as the ability to apply predefined tuple functions. Additionally, it notes the flexibility of defining lists within tuples and vice versa.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Nested Tuples and Lists in Python

The document explains the concept of nested tuples and lists in Python, detailing how to define one tuple inside another and the syntax involved. It provides examples demonstrating the indexing and slicing operations on inner tuples and lists, as well as the ability to apply predefined tuple functions. Additionally, it notes the flexibility of defining lists within tuples and vice versa.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

=============================================

Nested OR Inner tuple


=============================================
=>The Process of Defining One tuple Inside of Another tuple is called Inner OR
Nested tuple.
=>Syntax: tplobj=(Val1,Val2......(Val11,Val12....Val-1n), (Val21,Val22,...Val-
2n),....Val-n)
=>Here (Val1,Val2......,....Val-n) is called Outer tuple Elements
=>Here (Val11,Val12....Val-1n) is called Inner tuple Elements
=>Here (Val21,Val22,...Val-2n) is also another Inner tuple Elements.
=>On the Inner tuple Objects, we can also Perform Both Indexing and Slicing
Operations.
=>On the Objects of Inner tuple, we can apply all the Pre-defined Functions of
tuple( index(), count() only)
==============================================================================
Examples : Tuple in Tuple (Possibility 1)
==============================================================================
>>> t1=(10,"Rossum",(17,16,18),(77,78,66),"OUCET")
>>> print(t1,type(t1))------------(10, 'Rossum', (17, 16, 18), (77, 78, 66),
'OUCET') <class 'tuple'>
>>> t1[0]----------------------------10
>>> t1[1]----------------------------'Rossum'
>>> t1[2]----------------------------(17, 16, 18)
>>> t1[3]----------------------------(77, 78, 66)
>>> t1[2][1]------------------------16
>>> t1[-2][-1]----------------------66
============================================================================
Possibility 2: List in Tuple
============================================================================
=>Syntax: tplobj=(Val1,Val2......[Val11,Val12....Val-1n], [Val21,Val22,...Val-
2n],....Val-n)

=>Here (Val1,Val2......,....Val-n) is called Outer tuple Elements


=>Here [Val11,Val12....Val-1n] is called Inner list Elements
=>Here [Val21,Val22,...Val-2n] is also another Inner list Elements.
---------------------------------------
Examples
---------------------------------------
>>> t1=(10,"Rossum",[17,16,18],[77,78,66],"OUCET")
>>> print(t1,type(t1))---------------(10, 'Rossum', [17, 16, 18], [77, 78, 66],
'OUCET') <class 'tuple'>
>>> print(t1[2],type(t1[2]))-----------[17, 16, 18] <class 'list'>
>>> print(t1[3],type(t1[3]))-----------[77, 78, 66] <class 'list'>
>>> t1[2].sort()
>>> print(t1,type(t1))------------------(10, 'Rossum', [16, 17, 18], [77, 78, 66],
'OUCET') <class 'tuple'>
>>> t1[3].sort(reverse=True)
>>> print(t1,type(t1))------------------(10, 'Rossum', [16, 17, 18], [78, 77, 66],
'OUCET') <class 'tuple'>
==============================================================================
Possibility 3: tuple in list
============================================================================
=>Syntax: listobj=[Val1,Val2......(Val11,Val12....Val-1n), (Val21,Val22,...Val-
2n),....Val-n]

=>Here [Val1,Val2......,....Val-n] is called Outer list Elements


=>Here (Val11,Val12....Val-1n]) is called Inner tuple Elements
=>Here (Val21,Val22,...Val-2n) is also another Inner tuple Elements.
---------------------------------------
Examples
---------------------------------------
>>> l1=[10,"Rossum",(17,16,18),(77,78,66),"OUCET"]
>>> print(l1,type(l1))-----------------[10, 'Rossum', (17, 16, 18), (77, 78, 66),
'OUCET'] <class 'list'>
>>> l1[1]---------------------------------'Rossum'
>>> print(l1[2],type(l1[2]))---------(17, 16, 18) <class 'tuple'>
>>> print(l1[3],type(l1[3]))---------(77, 78, 66) <class 'tuple'>
==================================x============================================
NOTE:
--------
=>One can define One List in another List
=>One can define One Tuple in another Tuple
=>One can define One List in another Tuple ( tuple of lists)
=>One can define One tuple in another List (list of tuples)
===================================================================================
===

Common questions

Powered by AI

To manipulate elements of inner lists within a tuple, you can directly apply list functions since lists are mutable. For instance, you can sort the elements of a list inside a tuple using the sort() method. For example, given t1 = (10, 'Rossum', [17,16,18], [77,78,66], 'OUCET'), applying t1[2].sort() sorts the first inner list to [16, 17, 18].

A nested or inner tuple is a tuple defined inside another tuple. It is enclosed by parentheses '()' which allow the use of pre-defined tuple functions like index() and count() and support operations like indexing and slicing . An inner list within a tuple is a list contained in a tuple and is enclosed by square brackets '[]'. Unlike tuples, lists are mutable, meaning their elements can be reordered or changed, as demonstrated by using the sort() function .

In nested tuples, the indexing operation is used to access the elements. For example, given the tuple t1 = (10, 'Rossum', (17, 16, 18), (77, 78, 66), 'OUCET'), t1[2][1] accesses the second element of the first inner tuple, returning 16 . Indexing can be sequential, first selecting the inner tuple and then the specific element inside it.

Tuples in lists and lists in tuples differ mainly in mutability and element access. A tuple within a list allows its elements to remain immutable, while the list itself can be modified. For example, l1=[10, 'Rossum', (17,16,18), (77,78,66), 'OUCET'] includes immutable tuple elements . In contrast, a list within a tuple allows modification of the list's elements though the tuple structure is immutable. T1 = (10, 'Rossum', [17, 16, 18], [77, 78, 66], 'OUCET') shows lists being mutable where sort() can reorder list elements .

You might also like