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