# Python | Sort a tuple by its float element

In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting.

Examples:

```Input : tuple = [('lucky', '18.265'), ('nikhil', '14.107'),
('akash', '24.541'), ('anand', '4.256'), ('gaurav', '10.365')]
Output : [('akash', '24.541'), ('lucky', '18.265'),
('nikhil', '14.107'), ('gaurav', '10.365'), ('anand', '4.256')]

Input : tuple = [('234', '9.4'), ('543', '16.9'), ('756', '5.5'),
('132', '4.2'), ('342', '7.3')]
Output : [('543', '16.9'), ('234', '9.4'), ('342', '7.3'),
('756', '5.5'), ('132', '4.2')]
```

We can understand this from the image shown below: ## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 : Use of sorted() method

Sorted() sorts a tuple and always returns a tuple with the elements in a sorted manner, without modifying the original sequence. It takes three parameters from which two are optional, here we tried to use all of the three:

1. Iterable : sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
2. Key(optional) : A function that would server as a key or a basis of sort comparison.
3. Reverse(optional) : If set true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.

To sort this in ascending order we could have just ignored the third parameter.

 `# Python code to sort the tuples using float element ` `# Function to sort using sorted() ` `def` `Sort(tup): ` `    ``# reverse = True (Sorts in Descending order) ` `    ``# key is set to sort using float elements ` `    ``# lambda has been used ` `    ``return``(``sorted``(tup, key ``=` `lambda` `x: ``float``(x[``1``]), reverse ``=` `True``)) ` ` `  `# Driver Code ` `tup ``=` `[(``'lucky'``, ``'18.265'``), (``'nikhil'``, ``'14.107'``), (``'akash'``, ``'24.541'``),  ` `    ``(``'anand'``, ``'4.256'``), (``'gaurav'``, ``'10.365'``)] ` `print``(Sort(tup)) `

Output:

```[('akash', '24.541'), ('lucky', '18.265'), ('nikhil', '14.107'),
('gaurav', '10.365'), ('anand', '4.256')]
```

Method 2 : In-place way of sorting using sort():

While sorting via this method the actual content of the tuple is changed, while in the previous method the content of the original tuple remained the same.

 `# Python code to sort the tuples using float element ` `# Inplace way to sort using sort() ` `def` `Sort(tup): ` `    ``# reverse = True (Sorts in Descending order) ` `    ``# key is set to sort using float elements ` `    ``# lambda has been used ` `    ``tup.sort(key ``=` `lambda` `x: ``float``(x[``1``]), reverse ``=` `True``) ` `    ``print``(tup) ` ` `  `# Driver Code ` `tup ``=` `[(``'lucky'``, ``'18.265'``), (``'nikhil'``, ``'14.107'``), (``'akash'``, ``'24.541'``),  ` `    ``(``'anand'``, ``'4.256'``), (``'gaurav'``, ``'10.365'``)] ` `Sort(tup) `

Output:

```[('akash', '24.541'), ('lucky', '18.265'), ('nikhil', '14.107'),
('gaurav', '10.365'), ('anand', '4.256')]
```

For more reference visit:
sorted() in Python
lambda in Python

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6764973,"math_prob":0.525457,"size":3927,"snap":"2019-51-2020-05","text_gpt3_token_len":1112,"char_repetition_ratio":0.15115982,"word_repetition_ratio":0.23625557,"special_character_ratio":0.35981664,"punctuation_ratio":0.20480405,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97965133,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,9,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-08T03:16:25Z\",\"WARC-Record-ID\":\"<urn:uuid:e144fe3f-b0d2-4d7d-88e1-73db655aafcc>\",\"Content-Length\":\"133424\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0d7bf6f7-a47b-449a-9802-c480a8ec7d84>\",\"WARC-Concurrent-To\":\"<urn:uuid:016a1741-04ac-40ee-af5f-b8dfce1a690d>\",\"WARC-IP-Address\":\"23.221.72.19\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-sort-tuple-float-element/\",\"WARC-Payload-Digest\":\"sha1:V6UQUU2TFJ632CSWJNIKJQFGNUWL3SX5\",\"WARC-Block-Digest\":\"sha1:LI3DGVD6RZGTWN7LFOM3NP5NDR6DFNFX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540504338.31_warc_CC-MAIN-20191208021121-20191208045121-00306.warc.gz\"}"}