List: creating, indexing, accessing, range; methods (append, clear, copy, count, index, insert, pop, remove, reverse, sort)

Python lists लचीली, dynamic storage rows हैं जो अपने-आप फैलती हैं, कठोर C arrays को एक अकेले collection से बदलती हैं जो अपनी ख़ुद की resizing और sorting सँभालता है।

12 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Fixed Array की दीवार

आपके Semester 1 BCA104 C labs में, अगर आपको अपने रोज़ के campus expenses track करने थे, आपको int spends[30] जैसा एक सख़्त array declare करना पड़ता था। क्या होता अगर आप अचानक एक महीने में 31 purchases करते? आपका program एक segmentation fault या buffer overflow के कारण crash हो जाता। Python आपको एक ख़ाली collection बनाने क्यों देता है जो आपके चीज़ें ख़रीदने पर dynamically फैलता या सिकुड़ता है, कभी आपको पहले से एक memory capacity निर्दिष्ट करने पर मजबूर किए बिना?

Theory

Expandable Accordion Folder

एक Python list को एक लचीले accordion receipt folder की तरह सोचिए। जब आप इसे दुकान से ख़रीदते हैं, यह पूरी तरह ख़ाली और चपटा हो सकता है। हर बार जब आप इसमें एक नई expense receipt डालते हैं, जगह बनाने के लिए एक नया pocket अपने-आप खुल जाता है। आपको scale up करने के लिए folder फाड़ने या एक नया ख़रीदने की ज़रूरत नहीं। आप एक receipt बीच में slide कर सकते हैं, इसके slot number से किसी भी pocket के अंदर क्या है जाँच सकते हैं, या पीछे के margin से एक receipt साफ़-सुथरे बाहर खींच सकते हैं।

Theory

Python List औपचारिक रूप से

Python में एक list square brackets [] के अंदर लपेटा data elements का एक ordered, mutable collection है। C arrays के उलट, Python lists mixed data types store कर सकती हैं और memory allocation अपने-आप सँभालती हैं। आप individual elements को बाएँ से zero-based indexing, या दाएँ से negative indexing इस्तेमाल करके access करते हैं। elements का एक subset निकालने के लिए, आप एक slice range syntax list[start:stop] इस्तेमाल करते हैं, जो stop index सीमा तक पर उसे छोड़कर elements खींचता है।

At a glance

Table 1: ज़रूरी Python list modification methods और operations।

Method Toolकी गई ActionPocketMoney Impact
spends.append(val)list के बिल्कुल अंत में एक item जोड़ता है।spends.append(40) एक नई purchase जोड़ता है।
spends.insert(idx, val)एक ख़ास index slot पर एक item डालता है।spends.insert(0, 15) एक item को सामने मजबूर करता है।
spends.pop(idx)index पर item हटाता और return करता है (default आख़िरी)।last_spend = spends.pop() नवीनतम entry हटाता है।
spends.remove(val)एक ख़ास value की पहली घटना हटाता है।spends.remove(30) पहला samosa log हटाता है।
spends.sort()list items को जगह पर स्थायी रूप से sort करता है।spends.sort() history को सबसे कम से सबसे ज़्यादा order करता है।

Theory

Worked Example: Ledger Row सँभालना

आइए देखें कि हमारा PocketMoney application values के एक sequence को कैसे manipulate करता है। हम रोज़ के expenditures की एक baseline list से शुरू करेंगे, एक भूला हुआ transaction insert करेंगे, एक ख़ास value index खोजेंगे, order उलटेंगे, और अंतिम item साफ़-सुथरे pop out करेंगे।

Practical

PocketMoney Dynamic Ledger Operations

daily_spends = [50, 20, 120]

# Step 1: Append a late night snack expense
daily_spends.append(30)

# Step 2: Insert a missed morning tea expense at index 1
daily_spends.insert(1, 15)

# Step 3: Count how many times 20 appears
match_count = daily_spends.count(20)

# Step 4: Reverse the current layout order in-place
daily_spends.reverse()

# Step 5: Pop out the element now sitting at the front
popped_val = daily_spends.pop(0)

print("Final Spends Row:", daily_spends)
print("Popped item value:", popped_val)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

List Transformations को ट्रेस करें

ऊपर के Python script का मन में विश्लेषण कीजिए। execution पूरा होने पर console terminal पर कौन से बिल्कुल list elements और values दिखेंगे? tap करने से पहले हर method line ध्यान से ट्रेस कीजिए।

Show the answer

script output करेगी:

Final Spends Row: [120, 20, 15, 50]

Popped item value: 30

क्यों? कदम-दर-कदम ट्रेस करते हैं: Base list [50, 20, 120] है। append(30) के बाद, list [50, 20, 120, 30] है। insert(1, 15) के बाद, list [50, 15, 20, 120, 30] बनती है। reverse() के बाद, list पलटकर [30, 120, 20, 15, 50] हो जाती है। आख़िर में, pop(0) index 0 पर item हटाता है जो 30 है, [120, 20, 15, 50] पीछे छोड़ते हुए।

Quiz

sort() और reverse() जैसी list manipulation methods बनाम string methods के बीच एक बड़ा वैचारिक अंतर क्या है?

  1. List methods को पहले जटिल loop libraries import करने की ज़रूरत होती है।
  2. List methods collection को सीधे जगह पर modify करती हैं क्योंकि lists mutable हैं, immutable strings के उलट।
  3. List methods एक बिल्कुल नई list return करती हैं, original sequence को अपरिवर्तित छोड़ते हुए।
  4. List methods केवल integer collections process कर सकती हैं और text strings पर crash होती हैं।
Show the answer

List methods collection को सीधे जगह पर modify करती हैं क्योंकि lists mutable हैं, immutable strings के उलट।

क्योंकि Python lists mutable हैं, sort(), reverse(), और append() जैसी methods original list को सीधे memory में बदलती हैं और None return करती हैं। यह string methods के उलट है, जो original string को locked छोड़ती हैं और एक बिल्कुल नया text object return करती हैं।

Quiz

इस sequence पर विचार कीजिए: tracker = [10, 20, 30, 40]। एक theory exam paper में tracker[1:3] क्या return करेगा?

  1. [20, 30, 40]
  2. [10, 20]
  3. [20, 30]
  4. [10, 20, 30]
Show the answer

[20, 30]

List slicing range बिल्कुल string slicing की तरह बर्ताव करता है। यह start index शामिल करता है पर stop index बाहर रखता है। Index 1 20 की ओर इशारा करता है, index 2 30 की ओर, और index 3 बाहर रखा जाता है। इस तरह, यह sublist [20, 30] return करता है।

Watch out

Classic जाल: In-Place Return Void

university laboratory examinations में सबसे बार-बार marks-गँवाने वाली ग़लती ordered_spends = daily_spends.sort() जैसी एक assignment line लिखना है। क्योंकि sort method list को जगह पर स्थायी रूप से sort करती है, यह value None return करती है। आपका ordered_spends variable पूरी तरह ख़ाली हो जाता है, आपके sorted data तक access मिटाते हुए। अगर आप original list बदले बिना एक ताज़ा sorted copy चाहते हैं, आपको इसके बजाय built-in function sorted(daily_spends) इस्तेमाल करना होगा।

Theory

Collections को Semester 3 से जोड़ना

Lists database datasets shuffle करने के लिए मानक संरचनात्मक vehicles हैं। Semester 3 (BCA303) में, जब आप SQLite table rows से data matrices या records निकालते हैं, आपका Python connector script उन्हें tuples की lists के रूप में fetch करेगा, आपको multi-row outputs सहजता से process करने के लिए for record in transaction_list: जैसे loop patterns इस्तेमाल करने देते हुए।

Summary

Key takeaways

  • Lists mutable, ordered collections हैं जो memory में dynamically फैल या सिकुड़ सकती हैं।
  • Indices बाईं सीमा से शून्य पर और दाएँ margin से -1 पर शुरू होते हैं।
  • Slicing collections को एक start:stop pattern इस्तेमाल करके slice करता है जहाँ stop index बाहर रखा जाता है।
  • append और insert जैसी modification methods सक्रिय list variable को सीधे जगह पर mutate करती हैं।
  • pop tool elements को स्पष्ट index coordinates से हटाता है, जबकि remove मेल खाती value सामग्री से drop करता है।
  • Memory Hook: Strings नए objects देती हैं, list modifications जगह पर होती हैं!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Collections

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

List: creating, indexing, accessing, range; methods (append, clear, copy, count, index, insert, pop, remove, reverse, sort) · Programming Skills · Gri-Learn