Python Sort Lists
Sorting means arranging data in a particular order, usually ascending (smallest to largest) or descending (largest to smallest). Sorting helps in better data organization, faster searching, and easier analysis.
Sorting का मतलब है डेटा को किसी खास क्रम में व्यवस्थित करना, जैसे ascending (छोटे से बड़े) या descending (बड़े से छोटे) क्रम में। Sorting से डेटा की organization बेहतर होती है, तेज़ search होती है, और analysis आसान हो जाता है।
Why is sorting needed?
- To quickly find elements using methods like binary search.
- To organize data for reports and display.
- To make comparison and grouping easier.
- To optimize algorithms that require sorted data.
Sorting क्यों जरूरी है?
- Binary search जैसी तेज़ खोज के लिए।
- रिपोर्ट और display के लिए डेटा को व्यवस्थित करने के लिए।
- तुलना और grouping आसान बनाने के लिए।
- ऐसे algorithms के लिए जो sorted डेटा पर काम करते हैं।
Example 1: Sort List in Ascending Order Using sort()
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers)
Sorts the list in ascending order in-place.
List को ascending order में inplace sort करता है।
Output:
Example 2: Sort List in Descending Order Using sort() with reverse=True
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort(reverse=True)
print(numbers)
Sorts the list in descending order in-place.
List को descending order में inplace sort करता है।
Output:
Example 3: Using sorted() to Sort Without Modifying Original List
numbers = [3, 8, 1, 6]
sorted_numbers = sorted(numbers)
print('Original:', numbers)
print('Sorted:', sorted_numbers)
sorted() returns a new sorted list and leaves original list unchanged.
sorted() एक नई sorted list देता है, original list नहीं बदलता।
Output:
Sorted: [1, 3, 6, 8]
Example 4: Sort List of Strings Alphabetically
fruits = ['banana', 'apple', 'cherry']
fruits.sort()
print(fruits)
Sorts string list alphabetically (A to Z).
String list को alphabetical order (A से Z) में sort करता है।
Output:
Example 5: Sort List of Strings by Length
fruits = ['banana', 'apple', 'cherry', 'kiwi']
fruits.sort(key=len)
print(fruits)
Sorts strings by their length using key parameter.
Strings को उनकी लंबाई के अनुसार sort करता है।
Output:
Example 6: Sort List of Tuples by Second Item
items = [(1, 'b'), (2, 'a'), (3, 'c')]
items.sort(key=lambda x: x[1])
print(items)
Sorts list of tuples based on second element.
Tuples की list को दूसरे element के आधार पर sort करता है।
Output:
Example 7: Sort List in Descending Order Using sorted()
numbers = [4, 2, 7, 1]
sorted_desc = sorted(numbers, reverse=True)
print(sorted_desc)
Returns new list sorted in descending order.
Descending order में नई list return करता है।
Output:
Example 8: Sort List with Mixed Case Strings Case-Insensitive
fruits = ['banana', 'Apple', 'cherry', 'apple']
fruits.sort(key=str.lower)
print(fruits)
Sort strings ignoring case by using key=str.lower.
Case ignore करके strings को sort करता है।
Output:
Example 9: Sort List of Dictionaries by Key
students = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 20}]
students.sort(key=lambda x: x['age'])
print(students)
Sort list of dictionaries by the 'age' key.
'age' key के आधार पर dictionaries की list sort करें।
Output:
Example 10: Reverse a List
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers)
Reverse the elements of the list in-place.
List के elements को उलट देता है।
Output: