Lesson 11
Sorting Data
Order lists and dictionaries by key
Sorting lists and dictionaries is a vital skill in programming. It helps to organize data in a meaningful way, making it easier to analyze and understand.
2Sorting Lists🎯
To sort a list in Python, you can use the sort() method. This method sorts the list in place, meaning it changes the original list.
3Example:💡
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers) # Output: [1, 2, 5, 5, 6, 9]4Sorting in Reverse🚀
You can also sort lists in reverse order by passing the reverse parameter as True.
5Example:🔍
letters = ['d', 'a', 'c', 'b']
letters.sort(reverse=True)
print(letters) # Output: ['d', 'c', 'b', 'a']6Sorting Dictionaries⭐
Dictionaries can also be sorted, but you need to sort by keys or values. The sorted() function can help with this.
7Example:🧩
scores = {'Alice': 88, 'Bob': 95, 'Charlie': 78}
sorted_scores = sorted(scores.items())
print(sorted_scores) # Output: [('Alice', 88), ('Bob', 95), ('Charlie', 78)]8Sorting by Values🎈
If you want to sort a dictionary by its values, you can use a lambda function.
9Example:✨
sorted_by_value = sorted(scores.items(), key=lambda x: x[1])
print(sorted_by_value) # Output: [('Charlie', 78), ('Alice', 88), ('Bob', 95)]10Why Sorting Matters🎯
- ✓ Organizes data for better readability
- ✓ Makes searching for information faster
- ✓ Essential for data analysis and reporting
In this lesson, you've learned how to sort lists and dictionaries in Python. Sorting is a powerful tool that can help you manage your data more effectively!
🎯 Practice Challenges
Master Sorting Data by completing all 3 difficulty levels
Sorting Data - Easy Challenge
Start with the basics. Perfect for beginners!
Sorting Data - Medium Challenge
Get more challenging. Build your confidence!
Sorting Data - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Sorting Data? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 10: Enumerate & RangeLoop with indexes and custom sequences
- Next lesson →Lesson 12: Searching DataFind items efficiently in collections
- Grade 8 courseJavaScript Basics - Web ProgrammingJavaScript coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 7 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.