Merge and Sort Items of a Tuple in Python - Python Tutorial for Beginners

Published: 05 April 2021
on channel: Digital Academy
1,033
12

🎓Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Merge and Sort Items of a Tuple in Python

○ Merging two Tuples in Python

Although tuples are not changeable, you may sometimes want or need to merge two tuples, so that you can have all of your data located in the same variable. Or, even update its items, with new values. Fortunately, two tuples can be joined using the concatenation operator (+), or the augmented assignment operator (+=) to combine all of the items.

my_tuple = ('red', 'green', 'blue')

Concatenation operator
my_tuple = my_tuple + (1,2,3)
my_tuple = ('red', 'green', 'blue', 1, 2, 3)

Augmented assignment operator
my_tuple += (1,2,3)
my_tuple = ('red', 'green', 'blue', 1, 2, 3)

Notice: Tuples keep duplicate members, so this method does not overwrite existing values, and add them instead at the end.


○ Sorting a Tuple in Python

Awesome! But how would you proceed, if you need to rearrange items inside of your tuple? There are two methods to sort a tuple: using a built-in method, or convert it into a mutable object first, then rearrange it.

Method 1: Use the built-in sorted() method that accepts any sequence object as parameter.

my_tuple = ('red', 'green', 'blue', 'yellow')

my_tuple = tuple(sorted(my_tuple))
my_tuple = ('blue', 'green', 'red', 'yellow')

Method 2: Convert a tuple into a mutable object like list, gain access to a sorting method call, and convert it back into a tuple.

my_tuple = ('red', 'green', 'blue', 'yellow')

my_list = list(my_tuple) # Convert tuple to list
my_list.sort() # Sort the entire list
my_tuple = tuple(my_list) # Convert back to tuple

my_tuple = ('blue', 'green', 'red', 'yellow')


Let's play this video, stick around and watch until the end of this video! 👍🏻

Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, String...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assi...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF,...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Co...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


On this page of the site you can watch the video online Merge and Sort Items of a Tuple in Python - Python Tutorial for Beginners with a duration of hours minute second in good quality, which was uploaded by the user Digital Academy 05 April 2021, share the link with friends and acquaintances, this video has already been watched 1,033 times on youtube and it was liked by 12 viewers. Enjoy your viewing!