1. Learning Outcomes#
या chapter नंतर तुम्हाला:
list slicingम्हणजे list मधील एखादा भाग कसा निवडायचा ते समजेल.startआणिstopindex कसे काम करतात ते समजेल.stopindex result मध्ये का येत नाही हे स्पष्ट होईल.stepवापरून alternate किंवा ठराविक अंतराने elements कसे घ्यायचे ते समजेल.- negative indexes वापरून list च्या शेवटाकडील भाग कसा slice करायचा ते समजेल.
[::-1]वापरून list reverse कशी करायची ते समजेल.- मोठ्या list मधून useful sub-list कशी तयार करायची ते समजेल.
- slicing मधील common off-by-one आणि direction mistakes ओळखता येतील.
2. List Slicing ची गरज का पडते?#
समजा आपल्याकडे विद्यार्थ्यांच्या marks ची list आहे:
marks = [55, 68, 72, 81, 90, 76]आता पूर्ण list नको आहे. फक्त मधले तीन marks हवे आहेत.
आपण एकेक element वेगळा घेऊ शकतो, पण ते पटकन awkward होते. मोठ्या list मध्ये तर अजूनच त्रास होतो.
इथे slicing उपयोगी पडते.
list slicing म्हणजे list मधील सलग किंवा ठराविक pattern ने elements निवडून एक नवीन list मिळवणे.
उदाहरण:
marks = [55, 68, 72, 81, 90, 76]
selected = marks[1:4]
print(selected)Expected Output#
[68, 72, 81]इथे आपण index 1 पासून सुरुवात केली आणि index 4 च्या आधी थांबलो.
म्हणून indices 1, 2, आणि 3 वरील values मिळाल्या.
Slicing मध्ये start साधारणपणे included असतो आणि stop excluded असतो.
3. Basic List Slicing#
Basic slicing चे सर्वात सामान्य रूप असे आहे:
sub_list = original_list[start:stop]इथे:
start— कुठल्या index पासून घ्यायला सुरुवात करायची.stop— कुठल्या boundary च्या आधी थांबायचे.- result — निवडलेल्या elements ची list.
उदाहरण पाहू:
languages = ["Python", "Java", "SQL", "PHP", "JavaScript"]
print(languages[1:4])Expected Output#
['Java', 'SQL', 'PHP']चला index positions पाहू:
| Index | Value |
|---|---|
| 0 | Python |
| 1 | Java |
| 2 | SQL |
| 3 | PHP |
| 4 | JavaScript |
languages[1:4] मध्ये:
- index
1पासून selection सुरू होते. - index
4हा stop boundary आहे. - index
4वरील"JavaScript"घेतला जात नाही.
म्हणून result ['Java', 'SQL', 'PHP'] येतो.
4. Slicing चा Mental Model#
Slicing समजताना फक्त syntax पाठ करू नका. मनात एक छोटा flow ठेवा.
या flow मध्ये मुख्य गोष्ट अशी आहे:
- Python सुरुवातीची position ठरवतो.
- त्या position वरील element घेतो.
stepनुसार पुढच्या position कडे जातो.stopboundary आली की तिच्या आधी थांबतो.- निवडलेले elements result list मध्ये मिळतात.
आत्ता step दिला नसेल तर त्याची default value 1 असते.
म्हणजे Python एकेक index पुढे जातो.
5. Start आणि Stop Index#
Start index#
start म्हणजे selection कुठून सुरू होणार.
numbers = [10, 20, 30, 40, 50]
print(numbers[2:5])Expected Output#
[30, 40, 50]इथे start index 2 आहे.
Index 2 वरील value 30 result मध्ये आली.
म्हणून start boundary included आहे.
Stop index#
आता त्याच example मध्ये stop index 5 आहे.
List मध्ये valid indexes 0 ते 4 आहेत. तरी 5 stop म्हणून वापरता येतो, कारण stop हा element घ्यायचा index नसून boundary आहे.
Python index 5 च्या आधी थांबतो.
ही गोष्ट slicing ला खूप convenient बनवते.
उदाहरणार्थ पहिल्या तीन elements साठी:
data = ["A", "B", "C", "D", "E"]
print(data[0:3])Expected Output#
['A', 'B', 'C']Indices 0, 1, आणि 2 घेतले गेले.
Index 3 हा stop असल्यामुळे "D" घेतला गेला नाही.
start:stop मध्ये start घ्या, stop सोडा.
Stop excluded का ठेवला आहे?#
हे सुरुवातीला थोडे विचित्र वाटू शकते.
पण यामुळे slice ची length पटकन समजू शकते.
उदाहरण:
data[2:5]
इथे basic positive slicing मध्ये selected positions ची संख्या:
5 - 2 = 3
म्हणून indices 2, 3, 4 — एकूण तीन elements.
यामुळे adjacent slices लिहिणेही सोपे होते:
data = ["A", "B", "C", "D", "E", "F"]
first_part = data[0:3]
second_part = data[3:6]
print(first_part)
print(second_part)Expected Output#
['A', 'B', 'C']
['D', 'E', 'F']पहिला slice index 3 च्या आधी थांबतो.
दुसरा slice index 3 पासून सुरू होतो.
म्हणून overlap होत नाही आणि gap देखील राहत नाही.
6. Start किंवा Stop सोडून देता येतो#
Slicing मध्ये start आणि stop दोन्ही प्रत्येक वेळी लिहिणे आवश्यक नाही.
Start सोडला तर#
numbers = [10, 20, 30, 40, 50]
print(numbers[:3])Expected Output#
[10, 20, 30][:3] म्हणजे सुरुवातीपासून index 3 च्या आधीपर्यंत.
Stop सोडला तर#
numbers = [10, 20, 30, 40, 50]
print(numbers[2:])Expected Output#
[30, 40, 50][2:] म्हणजे index 2 पासून list च्या शेवटापर्यंत.
दोन्ही सोडले तर#
numbers = [10, 20, 30, 40, 50]
print(numbers[:])Expected Output#
[10, 20, 30, 40, 50][:] पूर्ण list चा slice घेतो.
या operation मुळे original list बदलत नाही.
Slicing हे list मधून selection करते. फक्त slice काढल्याने original list मधील elements बदलत नाहीत.
7. Slice Step#
आता slicing अधिक शक्तिशाली बनवूया.
पूर्ण syntax:
sub_list = original_list[start:stop:step]step म्हणजे एक element घेतल्यानंतर किती index positions ने पुढे जायचे.
Default step म्हणजे 1.
Step = 2#
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[0:6:2])Expected Output#
[10, 30, 50]इथे selected indexes आहेत:
| Move | Selected Index | Value |
|---|---|---|
| 1 | 0 | 10 |
| 2 | 2 | 30 |
| 3 | 4 | 50 |
Python index 0 पासून सुरू झाला.
नंतर step = 2 असल्यामुळे 0 → 2 → 4 असा पुढे गेला.
Index 6 हा stop boundary असल्यामुळे तिथे selection थांबली.
पूर्ण list मधून alternate elements#
Start आणि stop न देता फक्त step देता येतो:
letters = ["A", "B", "C", "D", "E", "F"]
print(letters[::2])Expected Output#
['A', 'C', 'E'][::2] म्हणजे:
- सुरुवातीपासून घ्या.
- शेवटापर्यंत जा.
- प्रत्येक वेळी दोन positions ने पुढे जा.
step = 2 म्हणजे "दोन elements घ्या" असा अर्थ नाही. त्याचा अर्थ पुढच्या selected position कडे जाताना दोन index positions ने move करा.
Step = 3#
values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(values[1:9:3])Expected Output#
[1, 4, 7]Start index 1.
नंतर 1 → 4 → 7.
पुढचा index 10 झाला असता, पण तो stop boundary 9 च्या पुढे आहे.
म्हणून result [1, 4, 7].
8. Negative Slicing#
Python मध्ये negative index वापरून list च्या शेवटाकडून positions मोजता येतात.
छोटा recap:
| Negative Index | Meaning |
|---|---|
| -1 | शेवटचा element |
| -2 | शेवटून दुसरा |
| -3 | शेवटून तिसरा |
उदाहरण:
numbers = [10, 20, 30, 40, 50]
print(numbers[-3:])Expected Output#
[30, 40, 50]-3 म्हणजे शेवटून तिसरी position.
म्हणून तिथून शेवटापर्यंत values मिळतात.
Negative start आणि negative stop#
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[-5:-2])Expected Output#
[20, 30, 40]हे समजण्यासाठी negative indexes ची left-side position मनात map करा:
| Value | Positive Index | Negative Index |
|---|---|---|
| 10 | 0 | -6 |
| 20 | 1 | -5 |
| 30 | 2 | -4 |
| 40 | 3 | -3 |
| 50 | 4 | -2 |
| 60 | 5 | -1 |
numbers[-5:-2] म्हणजे positive positions च्या दृष्टीने index 1 पासून index 4 च्या आधीपर्यंत.
म्हणून 20, 30, 40.
Negative index म्हणजे "उलट direction ने slice करा" असा अर्थ नाही. तो फक्त position शेवटाकडून मोजतो. Direction मुख्यतः step ठरवतो.
9. Negative Index असूनही Empty Slice का मिळू शकतो?#
हा beginner साठी खूप common confusion आहे.
पहा:
numbers = [10, 20, 30, 40, 50]
print(numbers[-1:-4])Expected Output#
[]"Start -1 आहे आणि stop -4 आहे, मग 50, 40, 30 मिळायला नको का?"
नाही, कारण इथे step दिलेला नाही.
Default step +1 आहे.
+1 म्हणजे Python उजवीकडे पुढे जाण्याचा प्रयत्न करतो.
पण -1 ही शेवटची position आहे आणि -4 boundary तिच्या डावीकडे आहे.
म्हणून positive direction ने त्या boundary कडे जाता येत नाही.
Result empty list येतो.
ही एक महत्त्वाची गोष्ट आहे:
- negative index position सांगतो.
- negative step direction बदलतो.
दोन्ही वेगळ्या गोष्टी आहेत.
10. Reversing a List with Slicing#
List reverse करण्यासाठी slicing मधील सर्वात प्रसिद्ध pattern आहे:
reversed_list = original_list[::-1]उदाहरण:
numbers = [10, 20, 30, 40, 50]
print(numbers[::-1])Expected Output#
[50, 40, 30, 20, 10][::-1] समजून घेऊ:
- start लिहिलेला नाही.
- stop लिहिलेला नाही.
- step
-1आहे.
Negative step असल्यामुळे traversal मागच्या direction ने होते.
म्हणून Python list च्या शेवटाकडून सुरुवात करून एकेक position मागे येतो.
क्रम:
50 → 40 → 30 → 20 → 10
आणि नवीन reversed result मिळतो.
[::-1] हे फक्त magic syntax म्हणून पाठ करू नका. त्यातील -1 हा step आहे, आणि तो traversal ची direction उलटी करतो.
काही portion reverse करणे#
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[4:1:-1])Expected Output#
[50, 40, 30]इथे:
- start index
4→50 - step
-1→ मागे जा - stop index
1→ index1वरील20घेऊ नका
म्हणून indices 4, 3, 2 निवडले जातात.
11. Creating Sub-Lists#
Slicing चा practical उपयोग म्हणजे मोठ्या list मधून लहान useful list तयार करणे.
समजा एका application मध्ये आठवड्याचे sales values आहेत:
sales = [1200, 1450, 1320, 1600, 1750, 1810, 1900]
weekend_view = sales[-2:]
print(weekend_view)Expected Output#
[1810, 1900]इथे पूर्ण list न वापरता शेवटच्या दोन entries ची sub-list घेतली.
दुसरे example:
students = ["Asha", "Ravi", "Neha", "Amit", "Pooja", "Kiran"]
middle_group = students[1:5]
print(middle_group)Expected Output#
['Ravi', 'Neha', 'Amit', 'Pooja']ही sub-list reports, batches, pagination-like views, sample processing, recent records, किंवा list चा एखादा भाग वेगळा पाहण्यासाठी उपयोगी पडते.
"थेंबे थेंबे तळे साचे" तसेच मोठ्या data structure वर काम करताना अनेकदा पूर्ण data न घेता लागणारा छोटा भाग निवडणे अधिक सोपे ठरते.
12. Slice Boundary List च्या बाहेर गेली तर?#
Normal indexing मध्ये चुकीचा index वापरल्यास IndexError येऊ शकतो.
पण slicing boundaries अनेकदा list च्या actual length पेक्षा पुढे असल्या तरी Python available elements घेऊन थांबतो.
उदाहरण:
numbers = [10, 20, 30]
print(numbers[1:100])Expected Output#
[20, 30]Stop 100 आहे, पण list तिथपर्यंत नाही.
Python available portion घेतो.
यामुळे slicing अनेक cases मध्ये convenient असते.
Slicing मध्ये out-of-range stop boundary वापरल्याने साधारणपणे error येत नाही; available range इतकाच result मिळतो.
13. Common Mistakes#
Mistake 1: Stop index ला included समजणे#
चुकीची अपेक्षा:
numbers[1:4] मध्ये indices 1, 2, 3, 4 मिळतील.
प्रत्यक्षात index 4 मिळत नाही.
योग्य rule:
start included, stop excluded.
Mistake 2: Step म्हणजे count समजणे#
numbers[::2] म्हणजे दोन elements घेणे नाही.
त्याचा अर्थ प्रत्येक selection नंतर दोन index positions ने move करणे.
Mistake 3: Negative index म्हणजे reverse direction समजणे#
numbers[-1:-4] आपोआप reverse होत नाही.
Default step अजूनही +1 आहे.
Reverse direction साठी negative step हवा.
Mistake 4: [::-1] पाठ करणे पण अर्थ न समजणे#
याचा अर्थ:
- full range
- negative step
- backward traversal
हे समजले की reverse slicing चे इतर examples देखील सोपे होतात.
Mistake 5: Empty slice म्हणजे error समजणे#
numbers = [10, 20, 30, 40]
print(numbers[2:2])Expected Output#
[]Start आणि stop boundary समान असल्यामुळे त्यांच्या मध्ये कोणताही selected index नाही.
म्हणून empty list मिळते.
14. स्वतःला Check करा#
values = [10, 20, 30, 40, 50] असल्यास values[1:4] मध्ये 50 का येत नाही?Show Answer
कारण stop index 4 excluded असतो. Slice indices 1, 2, आणि 3 घेतो. त्यामुळे result [20, 30, 40] येतो.
values[-1:-4] आणि values[-1:-4:-1] यांच्यात मुख्य फरक काय आहे?Show Answer
पहिल्या slice मध्ये default step +1 आहे, त्यामुळे traversal पुढच्या direction ने होते आणि left-side stop boundary कडे जाता येत नाही. दुसऱ्या slice मध्ये step -1 आहे, त्यामुळे traversal मागच्या direction ने होतो आणि elements निवडले जातात.
15. Interview Preparation#
What is list slicing in Python?Show Answer
List slicing is a way to extract a portion of a list using the syntax list[start:stop:step]. The start position is included, the stop boundary is excluded, and the step controls how indexes are traversed.
Key Points:
startis included.stopis excluded.stepcontrols traversal distance and direction.- Slicing returns selected elements as a list.
Why interviewer asks this:
It checks whether you understand Python sequence operations and boundary behavior instead of only memorizing syntax.
How do you reverse a list using slicing?Show Answer
Use list[::-1]. The omitted start and stop cover the full list, and the step -1 traverses the list backward.
Key Points:
-1is the step.- Negative step means backward traversal.
- The original list is not changed by simply creating this slice.
Why interviewer asks this:
It checks your understanding of the step component and negative traversal.
Why is the stop index excluded in Python slicing?Show Answer
An exclusive stop boundary makes slice lengths and adjacent ranges easier to reason about. For a positive step of 1, the length of a simple slice from start to stop is commonly stop - start when the boundaries are valid and ordered.
Key Points:
- The stop boundary is not selected.
- Adjacent slices can meet cleanly at the same boundary.
- Simple range lengths are easier to calculate.
Why interviewer asks this:
It checks whether you understand the design behavior behind slicing rather than only its syntax.
16. Recap#
या chapter मधून हे पक्के लक्षात ठेवा:
- Basic slicing:
list[start:stop] - Full form:
list[start:stop:step] startincluded असतो.stopexcluded असतो.- start सोडला तर slicing सुरुवातीपासून सुरू होऊ शकते.
- stop सोडला तर slicing शेवटापर्यंत जाऊ शकते.
step = 2म्हणजे प्रत्येक दुसऱ्या position कडे move करणे.- negative index list च्या शेवटाकडून position सांगतो.
- negative step traversal ची direction उलटी करतो.
[::-1]पूर्ण list reverse करण्याचा slicing pattern आहे.- slicing वापरून मोठ्या list मधून useful sub-list सहज तयार करता येते.
- slice boundaries आणि traversal direction समजली की slicing पाठ करण्याची गरज कमी होते.