For Loop With Tuples

This article covers ‘for’ loops and how they are used with tuples. Even if the tuples are immutable, the accessibility of the tuples is similar to that of the list.

This article covers ‘for’ loops and how they are used with tuples. Even if the tuples are immutable, the accessibility of the tuples is similar to that of the list. Traverse through a tuple with a for loop.

Python
Python
Python
tuples=()
tuples=(1,'user@gmail.com',5.2,'User',True)

for i in tuples:
    print(i)

Output
1
user@gmail.com
5.2
User
True
Python

Use enumerate with tuple to get index and value.

Python
Python
Python
for i,j in enumerate(tuples):
    print(i,j)
Output
0 1
1 user@gmail.com
2 5.2
3 User
4 True
Python

To get this output without enumerating, we have to change codes as follows.

Python
Python
Python
size=len(tuples)
# i will be used as index pointer here,
for i in range(size):
    print(i,tuples[i])
Output
0 1
1 user@gmail.com
2 5.2
3 User
4 True
Python

Nested Tuples

Let’s create a pseudo-dataset within a nested structure, a tuple. If we print it directly, it will not show up in table format.

Python
Python
Python
ntuple=(
    ('id','sal','name','email','married'),
    (1,5.2,'User1','user1@gmail.com',True),
    (2,4.8,'User2','user2@gmail.com',True),
    (3,6.3,'User3','user3@gmail.com',True)
)
print(ntuple)
Output
(('id', 'sal', 'name', 'email', 'married'), (1, 5.2, 'User1', 'user1@gmail.com', True), (2, 4.8, 'User2', 'user2@gmail.com', True), (3, 6.3, 'User3', 'user3@gmail.com', True))
Python

Let’s print the nested tuple in table format.

Python
Python
Python
for i in ntuple:
    for j in i:
        print(j,end=' \t ')
    print()
Output
id 	 sal 	 name 	 email	 	        married 	 
1 	 5.2 	 User1 	 user1@gmail.com 	 True 	 
2 	 4.8 	 User2 	 user2@gmail.com 	 True 	 
3 	 6.3 	 User3 	 user3@gmail.com 	 True 
Python

Tuple Unpacking

We will use the same tuple to demonstrate the tuple unpacking.

Python
Python
Python
ntuple=(
    ('id','sal','name','email','married'),
    (1,5.2,'User1','user1@gmail.com',True),
    (2,4.8,'User2','user2@gmail.com',True),
    (3,6.3,'User3','user3@gmail.com',True)
)
print(ntuple)
Output
(('id', 'sal', 'name', 'email', 'married'), (1, 5.2, 'User1', 'user1@gmail.com', True), (2, 4.8, 'User2', 'user2@gmail.com', True), (3, 6.3, 'User3', 'user3@gmail.com', True))
Python

Let’s try to unpack a tuple without any special method. Let’s use for loop to unpack tuple.

Python
Python
Python
for i in ntuple:
    j,k,l,m,n=i
    print(j,k,l,m,n)
Output
id	sal	name	email	married	
1	5.2	User1	user1@gmail.com	True	
2	4.8	User2	user2@gmail.com	True	
3	6.3	User3	user3@gmail.com	True	
Python
Python
Python
Python
i,*j=ntuple
print(i,j,sep='\n')
Output
('id', 'sal', 'name', 'email', 'married')
[(1, 5.2, 'User1', 'user1@gmail.com', True), (2, 4.8, 'User2', 'user2@gmail.com', True), (3, 6.3, 'User3', 'user3@gmail.com', True)]
Python

This is how a tuple is unpacked without a for loop.

Python
Python
Python
i,j,*k=ntuple
print(i,j,k,sep='\n')
Output
('id', 'sal', 'name', 'email', 'married')
(1, 5.2, 'User1', 'user1@gmail.com', True)
[(2, 4.8, 'User2', 'user2@gmail.com', True), (3, 6.3, 'User3', 'user3@gmail.com', True)]
Python

Practice Your codes here

ntuple=( (‘id’,’sal’,’name’,’email’,’married’), (1,5.2,’User1′,’user1@gmail.com’,True), (2,4.8,’User2′,’user2@gmail.com’,True), (3,6.3,’User3′,’user3@gmail.com’,True) ) print(ntuple) i,j,*k=ntuple print(i,j,k,sep=’\n’)

How useful was this post?

Click on a star to rate it!

Leave a Reply

Points You Earned

Untitled design 6
0 distinction_points
Untitled design 5
python_points 0
0 Solver points
Instagram
WhatsApp
error: Content is protected !!