Python Tutorial: Sorting Lists, Tuples, and Objects

///Python Tutorial: Sorting Lists, Tuples, and Objects

Python Tutorial: Sorting Lists, Tuples, and Objects

FavoriteLoadingAdd to favorites

In this video we will be learning how to sort lists, tuples, and other objects in Python. We will start by performing simple sorts on a list of integers and then move on to slightly more advanced sorting using custom criteria. Let’s get started.

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2020-02-10T01:44:20+00:00February 10th, 2020|Python Video Tutorials|41 Comments

41 Comments

  1. George Gordon February 10, 2020 at 1:44 am - Reply

    @Corey Schafer
    First of all thank you for the videos and the amazing explanations!!! They have been a wonder. I would like to however find out what settings or packages you use to get the info on what it is that you are typing when coding. For example in this video at 0.56 while you are typing " s_li "…. it indicates that it is a statement. I have also seen that it does this for keywords and others as well. I have watched your video in configuring sublime text for python and have added the packages that work for me and have done the settings, however those settings have not added this functionality. Could you point me in the right direction so i can configure my sublime text to allow for this? Thank you

  2. Usman Malik February 10, 2020 at 1:44 am - Reply

    sum of digits of a number until sum becomes single digit_pl make a video on that

  3. Faizan Hassan February 10, 2020 at 1:44 am - Reply

    The ease with which this person writes the code is ultra classy. I can only wish I had the knowledge as superior as Corey's.

  4. Vivek Subramanian February 10, 2020 at 1:44 am - Reply

    2:07 should be “sort” method, not “sorted”

  5. William February 10, 2020 at 1:44 am - Reply

    on the function "e_sort()", how does salary, age or name is returned from the class?

  6. sai charan February 10, 2020 at 1:44 am - Reply

    how is he printing without paranthesis ?
    anyone …

  7. Кирилл Кириленко February 10, 2020 at 1:44 am - Reply

    Hey friend! Thx for you work. Can you explain me how do you delete # symbol in SublimeText with a single click? I use mac OS and I Don't know the hotkeys to delete symbols in the few string at once

  8. CHINMAY JAPE February 10, 2020 at 1:44 am - Reply

    Hello,
    Why we use init and repr as _init_ and __repr__… What is the logic behind putting the _ before and after them?

  9. Sahith Vokkerla February 10, 2020 at 1:44 am - Reply

    Hey everybody, how's it going in comment section?

  10. kush khandelwal February 10, 2020 at 1:44 am - Reply

    I want to know how can I sort values in dictionary.

  11. Razed surazed February 10, 2020 at 1:44 am - Reply

    sorry i wanna ask, can i sorting value of list without change an index

  12. Soumya Ranjan Das February 10, 2020 at 1:44 am - Reply

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  13. Victor Yu February 10, 2020 at 1:44 am - Reply

    Is there a video that explains what "class" is and what _init_ means?

  14. sharun kumar February 10, 2020 at 1:44 am - Reply

    Hello Corey, you've been doing a great job helping people to gain tremendous amount of knowledge, I will certainly make a huge contribution to your channel once I find a job as a python developer.
    Also, I have one suggestion …..
    Why not release your compiled course on Udemy, its a huge community and it would help you earn well as each student purchases your course.

    Cheers
    God bless you brother
    Lots of respects to you from India

  15. König TV February 10, 2020 at 1:44 am - Reply

    I've got this list called r and I have to sort it in a function. If I use r.sort() or t = sort(r) it's still not sorted.
    Can somebody explain to me why that is and how to fix it?

  16. Jayce Azua February 10, 2020 at 1:44 am - Reply

    So with sorting objects what if the age is the same integer value, how can we switch it to sort based on the name alphabetically

  17. ProgRAMmer February 10, 2020 at 1:44 am - Reply

    done 9/9/19

  18. alemapnil L February 10, 2020 at 1:44 am - Reply

    You didn't assign anything to emp, how does computer know what emp is?

  19. Mateusz Smendowski February 10, 2020 at 1:44 am - Reply

    Amazing. Thanks to you, I have learned so much when I discovered your channel. My grandpa had told me a long time ago: "Knowing how to do something
    is not valuable, but knowing how to do something and why to do this in such a way is.". You give us pure knowledge. Thank you.

  20. Dopeboyz789 February 10, 2020 at 1:44 am - Reply

    How do you match or intersection two multidimensional list of a list

  21. Garry C February 10, 2020 at 1:44 am - Reply

    Great vid. How about a video on linked-list vs. arrays, and sort algorithms?

  22. TUSHAR BOMMALA February 10, 2020 at 1:44 am - Reply

    Hey Corey, I need to sort two arrays
    1. contains the chemical reactions
    2.sensitivity values
    The array is the size of 325. I was unable to solve that can u please me out?
    I will be glad if u reply to my comment

    Thanks

  23. Jamal Jameezy February 10, 2020 at 1:44 am - Reply

    For those reading the comments: this channel is a goldmine!

  24. O February 10, 2020 at 1:44 am - Reply

    you are the best, you help me a lot TNX.

  25. Bing Wong February 10, 2020 at 1:44 am - Reply

    Hi, thank you for all your videos. How do you comments out a line or several lines?

  26. NOYB February 10, 2020 at 1:44 am - Reply

    Hello Corey,
    These videos are fantastic and I am learning so much so thank you for posting them. I hope you are still monitoring them because I have a question. I was attempting to sort a dictionary by its values rather than its keys and I am getting some errors. My original dictionary was structured as {string:string} and when I attempted to sort it I received the following error: TypeError: '<' not supported between instances of 'int' and 'str'. I then created a new dict with {string:int} and the sorting worked fine. I searched for the issue but every example has a dictionary with {string:int}. I am at a loss as to how to sort a dictionary with {string:string}, am I missing something very simple. I used the following sort methods: sorted_dict = sorted(dict_01.items(), key=operator.itemgetter(1)) and sorted_dict = sorted(dict_01.items(), key=lambda x: x[1]). Appreciate any insight you could offer!!

  27. videofountain February 10, 2020 at 1:44 am - Reply

    I am wondering if you should create a companion video for multiple key sort.

  28. wesam mustafa February 10, 2020 at 1:44 am - Reply

    you are amazing, thanks for these awesome videos

  29. Brajesh Narayan February 10, 2020 at 1:44 am - Reply

    How he is removing hash '#' in just one click?

  30. Kon Khmer February 10, 2020 at 1:44 am - Reply

    Hi sir I use Pycharm for coding, but some keyword are not defined, and I try to use sublime text also but it still don't work. e.g print nums # which don't have parentheses.

  31. Daniel Henry February 10, 2020 at 1:44 am - Reply

    If anyone needs the source code of last example:
    class Employee:

    def __init__(self, name, age, salary):

    self.name = name

    self.age = age

    self.salary = salary

    def __repr__(self):

    return '({}, {}, {})'.format(self.name, self.age, self.salary)

    e1 = Employee("Carl", 37, 70000)

    e2 = Employee("Sarah", 29, 80000)

    e3 = Employee("John", 43, 90000)

    employees = [e1, e2, e3]

    # def byName(emp):

    # return emp.name

    # you can sort by age and salary by returning:

    # return emp.age

    # return emp.salary

    s_employees = sorted(employees, key=lambda e: e.salary, reverse=True)

    print(s_employees)

    # or

    class Employee:

    def __init__(self, name, age, salary):

    self.name = name

    self.age = age

    self.salary = salary

    def __repr__(self):

    return '({}, {}, {})'.format(self.name, self.age, self.salary)

    e1 = Employee("Carl", 37, 70000)

    e2 = Employee("Sarah", 29, 80000)

    e3 = Employee("John", 43, 90000)

    employees = [e1, e2, e3]

    from operator import attrgetter

    s_employees = sorted(employees, key=attrgetter("salary"), reverse=True)

    print(s_employees)

  32. Daniel Henry February 10, 2020 at 1:44 am - Reply

    very concise and to the point. thank you sir

  33. Alex Bordei February 10, 2020 at 1:44 am - Reply

    If I had a large data set with many attributes, is there a way that I could turn them all into objects in bulk, so that I could then proceed to sorting the data in a certain way?

  34. Shubham Kumar February 10, 2020 at 1:44 am - Reply

    can any1 plzz tell me where is e_sort() is called and how emp is passed as a argument

  35. anon February 10, 2020 at 1:44 am - Reply

    key=lambda x: x.salary

    edit: lol i just realized i only watched half the video.. and now you are showing the lambda function haha

  36. Мария Полякова February 10, 2020 at 1:44 am - Reply

    Thank you very much, mate! This video really helped me!

  37. pgo eds February 10, 2020 at 1:44 am - Reply

    0:18 "a custom criteria" ? criteria = plural of criterion

  38. Techstudio DN February 10, 2020 at 1:44 am - Reply

    I wonder how can someone dislike these tutorials

  39. YogaFinder Worldwide February 10, 2020 at 1:44 am - Reply

    If you want name as primary sort and then age as secondary sort…how would you do that?

  40. aperxmim February 10, 2020 at 1:44 am - Reply

    This is a lot of stuff!

  41. Sam Woodhams February 10, 2020 at 1:44 am - Reply

    Loving the series and wanted to say thank you for putting this out. I noticed due to a typo in my hand typing/copying the code and running it that it is worth mentioning that like other languages I have looked at (which would be just barely getting into C and basic) that sorting strings is based upon the ASCII set, which means that sorting the words 'OS' and 'age' due to lowercase being higher numbered the 'age' string is returned after the 'OS' string. These videos are awesome, TY again for doing this

Leave A Comment

*