Reverse a List using Slicing

Reverse a List using Slicing

Ist = [‘a’,’b’, ‘c’,’d’]
print (“Before reversing list items”)
print (Ist)
Ist = Ist [::-1]
print (“After reversing list items”)
print (Ist)

Output:
Before reversing list items
[‘a’, ‘b’, ‘c’, ‘d’]
After reversing list items
[‘d’, ‘c’, ‘b’, ‘a’]

Accessing Individual Elements in Reversed Order
रिवर्स ऑर्डर में व्यक्तिगत एलीमेंट तक पहुँचना

If you need to access individual elements of a list in reverse order, it’s better to use reversed () method. reversed () method returns an iterator that accesses the given sequence in the reverse order.

यदि आपको किसी लिस्ट के अलग-अलग एलीमेंट को रिवर्स ऑर्डर में एक्सेस करने की आवश्यकता है, तो reversed() मेथड का उपयोग करना बेहतर है। reversed() मेथड एक पुनरावृत्ति देता है जो दिए गए सिक्वेन्स को रिवर्स ऑर्डर में एक्सेस करता है।

Syntax: reversed(sequence)

Example: Python code to demonstrate working of reversed () function.
पायथन कोड reversed() फंक्शन के काम को प्रदर्शित करने के लिए।
# For string
str = ‘god’
print (list (reversed (str)))
# For tuple
tpl = (‘m’, ‘a’, ‘s’, ‘t’, ‘e’, ‘r’)
print (list (reversed (tpl)))
# For range
rng = range (1, 5)
print (list (reversed (rng)))
# For list
Ist = [1, 2, 4, 3, 5]
print (list (reversed (Ist)))

Output:
[‘d’, ‘o’, ‘g’]
[‘r’, ‘e’, ‘t’, ‘s’, ‘a’, ‘m’]
[4, 3, 2, 1]
[5, 3, 4, 2, 1]

sort(): The sort() method sorts the elements of a given list in a specific order – Ascending or Descending.

sort() मेथड किसी विशिष्ट क्रम में दिए गए लिस्ट के एलीमेंट को क्रमबद्ध करती है- आरोही या अवरोही।

Syntax: list.sort(key=…, reverse=…)

reverse – If true, the sorted list is reversed (or sorted in Descending order)

यदि सही है, तो क्रमबद्ध लिस्ट रिवर्स है (या अवरोही क्रम में क्रमबद्ध है)

key – function that serves as a key for the sort comparison

फंक्शन जो क्रमबद्ध कम्पेरिजन के लिए एक कुंजी के रूप में कार्य करता है

sort() doesn’t return any value.

sort) किसी भी वैल्यू को रिटर्न नहीं करता है।

For numbers: Default natural sorting order is Ascending order.

संख्याओं के लिए: डिफॉल्ट नैचुरल शॉर्टिंग क्रम आरोही क्रम है

For strings: Default natural sorting order is Alphabetical order.
स्ट्रिंग के लिए: डिफॉल्ट नैचुरल शॉर्टिंग क्रम वर्णमाला क्रम है

Example 1:
>>> x = [2, 5,3,90, 12, 67]
>>> x.sort ()
>>> x =[2, 3, 5, 12, 67, 90]
>>> y = [ ‘ zibra’, ‘pink’, ‘apple’, ‘boy’ ]
>>> y.sort ()
>>> y =[‘apple’, ‘boy’, ‘pink’, ‘zibra’]
>>> print (y.sort())
None

#sort () doesn ‘ t return any value, so that none is the result.

Example 2: reverse=True sorts the list in the descending order.
nums = [70,50,60,1, 20]
print(“Original list : “, nums)
nums.sort (reverse=True)
print(‘Sorted (Descending order) list : ‘, nums)

Output:
Original list :  [70, 50, 60, 1, 20]
Sorted (Descending order) list :  [70, 60, 50, 20, 1]

Alternatively, you can also use Python’s in-built function sorted() for the same purpose.

वैकल्पिक रूप से, आप एक ही उद्देश्य के लिए पायथन के इन-बिल्ट फंक्शन sorted() का भी उपयोग कर सकते हैं।

Syntax: sorted(iterable[, key][, reverse])

sorted() takes two three parameters: – sorted() दो तीन पैरामीटर लेता है:

iterable – sequence (string, tuple, list) or collection (set, dictionary, frozen set) or any iterator

सिक्वेन्स (स्ट्रिंग, टपल, लिस्ट) या कलेक्शन (सेट, डिक्शनरी, फ्रोजेन सेट) या कोई पुनरावृत्ति

reverse (Optional) – If true, the sorted list is reversed (or sorted in Descending order)

यदि सही है, तो सॉर्ट की गई लिस्ट रिवर्स है (या अवरोही क्रम में सॉर्ट की गई है।)

key (Optional) – function that serves as a key for the sort comparison

फंक्शन तुलना के लिए एक कुंजी के रूप में कार्य करता है।

sorted() method returns a sorted list from the given iterable.
sorted() मेथड दिए गए पुनरावृत्ति से एक क्रमबद्ध लिस्ट देता है।

Example 3:
nums = [70,50,60,1,20]
print (“Original list : “, nums)
print (‘Sorted (Ascending order) list : ‘, sorted (nums))

Output:
Original list :  [70, 50, 60, 1, 20]
Sorted (Ascending order) list :  [1, 20, 50, 60, 70]

Example 4: reverse=True sorts the list in the descending order.
रिवर्स= टू लिस्ट को अवरोही क्रम में क्रमबद्ध करता है।
nums = [70, 50, 60,1, 20]
print (“original list : “, nums)
print (‘Sorted (Descending) list : ‘, sorted (nums, reverse=True))

Output:
Original list :  [70, 50, 60, 1, 20]
Sorted (Descending) list :  [70, 60, 50, 20, 1]

Note: In Python 2 if list contains both numbers and strings then sort() function first sort numbers and followed by string. But in Python 3 it is invalid.

नोट: पायथन 2 में यदि लिस्ट में नम्बर और स्ट्रिंग दोनों हैं, तो sort() फंक्शन पहले शॉर्ट नम्बर और उसके बाद स्ट्रिंग को शॉर्ट करें। लेकिन पायथन 3 में यह अमान्य है।

Example 5: If list contains both numbers and strings then sort() and sorted() raises an error
message. – यदि लिस्ट में नम्बर और स्ट्रिंग दोनों हैं तो sort() और sorted() एक एरर मैसेज रेज करता है।
nums = [‘apple’, 50, 60, ‘banana’, 20]
print (“Original list: “, nums)
nums.sort()
print(‘Sorted list : ‘, nums)

Output:
Original list:  [‘apple’, 50, 60, ‘banana’, 20]
Traceback (most recent call last):
  File “C:\Users\HP\Desktop\1.py”, line 3, in <module>
    nums.sort()
TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’

Note: Difference between sort() and sorted() is: sort() doesn’t return any value while, sorted() returns an iterable list.

नोट: sort() और sorted() के बीच अंतर है: sort() कोई वैल्यू रिटर्न नहीं करता है, जबकि sorted() एक पुनरावृत्त सूची देता है।

Aliasing and Cloning of List objects –
लिस्ट ऑब्जेक्ट्स का अलियांसिंग और क्लोनिंग

The process of giving another reference variable to the existing list is called aliasing.

मौजूदा लिस्ट में एक ओर संदर्भ वैरियेबल देने की प्रक्रिया को अलियांसिंग कहा जाता है।
Example 1:
x = [10, 20, ‘ zero’, ‘one’]
y = x
print (“id of x : “, id (x))
print (“id of y: “, id (y))

Output:
id of x :  55971624
id of y :  55971624

The problem in this approach is by using one reference variable if we are changing content, then those changes will be reflected to the other reference variable.

इस दृष्टिकोण में समस्या एक संदर्भ वैरियेबल का उपयोग करती है, यदि हम सामग्री बदल रहे हैं, तो उन परिवर्तनों को अन्य संदर्भ वैरियेबल पर प्रतिबिंबित किया जाएगा।

Example 2:
x = [10, 20, ‘zero’, ‘one’]
y = x
print(“id of x : “, id (x) )
print (“id of y : “, id (y))
y[0] = 30
print (y)
print (x)

Output:
id of x :  52104936
id of y :  52104936
[30, 20, ‘zero’, ‘one’]
[30, 20, ‘zero’, ‘one’]

To overcome this problem we should go for cloning. The process of creating exactly duplicate independent object is called cloning. We can implement cloning by using slice operator or by using copy() function.

इस समस्या को दूर करने के लिए हमें क्लोनिंग के लिए जाना चाहिए। बिल्कुल डुप्लीकेट इंडिपेंडेंट ऑब्जेक्ट बनाने की प्रक्रिया को क्लोनिंग कहा जाता है। हम स्लाइस ऑपरेटर का उपयोग करके या copy() फंक्शन का उपयोग करके क्लोनिंग को लागू कर सकते हैं।

Example 3: By using slice operator – स्लाईस ऑपरेटर का उपयोग करके

x = [10, 20,30]
y = x [:]
print(“id of x. : “, id(x))
print (“id of y : “, id (y))
y[1] = 153
print (y)
print (x)

Output:
id of x. :  48631432
id of y :  9097352
[10, 153, 30]
[10, 20, 30]

Example 4: by using copy() function
copy() फंक्शन का उपयोग करके

x = [10, 20,30]
y = x. copy ()
print(“id of x : “, id(x))
print(“id of y : “, id (y))
y[1] = 15
print (y)
print (x)

Output:
id of x :  55578312
id of y :  14143624
[10, 15, 30]
[10, 20, 30]

Note: Difference between ‘=’ operator and copy() function: ‘=’ operator meant for aliasing and copy() function meant for cloning().

नोट: ‘=’ ऑपरेटर और copy() फंक्शन में अंतर: ‘=’ ऑपरेटर का मतलब अलियासिंग और copy() फंक्शन cloning() के लिए होता है।

Chapter wise Model Paper Link

About Me