List Indexing
List Indexing అంటే ఏమిటి?
Listలో ఉన్న ఒక ప్రత్యేకమైన itemను దాని position ద్వారా access చేయడాన్ని List Indexing అంటారు.
Pythonలో list index సంఖ్య 0తో ప్రారంభమవుతుంది. అంటే మొదటి item index 0, రెండవ item index 1, మూడవ item index 2గా ఉంటుంది.
Indexing ఎందుకు ఉపయోగిస్తాము?
Listలోని మొత్తం items కాకుండా, అవసరమైన ఒక itemను మాత్రమే చదవడానికి indexing ఉపయోగిస్తాము. ఉదాహరణకు, fruits listలో మొదటి fruit లేదా మూడవ fruitను print చేయవచ్చు.
Indexing ఎలా పనిచేస్తుంది?
List పేరు తర్వాత square bracketsలో index numberను రాయాలి.
list_name[index]
ఉదాహరణకు fruits[0] అంటే fruits listలోని మొదటి item. fruits[2] అంటే మూడవ item.
Negative Indexing
Pythonలో చివరి itemను -1 indexతో access చేయవచ్చు. చివరి itemకు ముందు ఉన్న itemను -2తో access చేయవచ్చు.
list[-1]– చివరి itemlist[-2]– చివరి itemకు ముందు ఉన్న item
ఎక్కడ ఉపయోగపడుతుంది?
Student namesలో ఒక studentను తీసుకోవడం, products listలో ఒక productను చదవడం, చివరి orderను access చేయడం వంటి సందర్భాల్లో list indexing ఉపయోగపడుతుంది.
Simple Explanation
ఒక listలోని itemsకు ప్రతి ఒక్కదానికి ఒక index number ఉంటుంది. Pythonలో counting 0తో మొదలవుతుంది.
కాబట్టి listలోని మొదటి itemను పొందడానికి list[0], రెండవ itemకు list[1] ఉపయోగించాలి. చివరి itemను సులభంగా పొందడానికి list[-1] ఉపయోగించవచ్చు.
గుర్తుంచుకోండి: index అనేది item యొక్క positionను సూచిస్తుంది, కానీ మొదటి positionకు Pythonలో 0 ఉంటుంది.
Real-Life Example
ఒక shelfపై నాలుగు పుస్తకాలు వరుసగా ఉన్నాయని అనుకోండి. Pythonలో మొదటి పుస్తకానికి position 0, రెండవ పుస్తకానికి position 1 ఉంటుంది. అలాగే, shelfలో చివరి పుస్తకాన్ని చివరి position తెలియకపోయినా -1తో access చేయవచ్చు.
Code Examples
Listలో itemsను index ద్వారా access చేయడం
fruits = ["apple", "banana", "mango", "orange"]
print(fruits[0])
print(fruits[2])
print(fruits[-1])
ఈ codeలో fruits listలోని మొదటి item, మూడవ item మరియు చివరి itemను indexing ద్వారా print చేస్తున్నాము. 0 మొదటి itemను, 2 మూడవ itemను, -1 చివరి itemను సూచిస్తుంది.
apple mango orange
వేర్వేరు indexesను ఉపయోగించడం
colors = ["red", "green", "blue"]
print(colors[1])
print(colors[-2])
colors[1] రెండవ itemను ఇస్తుంది. colors[-2] చివరి itemకు ముందు ఉన్న itemను ఇస్తుంది.
green green
Line-by-Line Explanation
ఈ lineలో నాలుగు fruit namesతో fruits అనే list create అవుతుంది. Python ఈ itemsను index 0 నుంచి index 3 వరకు ఉంచుతుంది.
ఇక్కడ fruits listలో index 0 ఉన్న itemను access చేస్తున్నాము. Index 0 మొదటి itemను సూచిస్తుంది కాబట్టి apple outputగా వస్తుంది.
ఇక్కడ index 2 ఉన్న itemను access చేస్తున్నాము. Counting 0 నుంచి మొదలవుతుంది కాబట్టి index 2 మూడవ item, అంటే mangoను సూచిస్తుంది.
ఇక్కడ -1 indexను ఉపయోగిస్తున్నాము. Pythonలో -1 ఎల్లప్పుడూ listలోని చివరి itemను సూచిస్తుంది కాబట్టి orange outputగా వస్తుంది.
Try It
Try this Python code:
colors = ["red", "green", "blue"]
print(colors[1])
print(colors[-1])
Practice
Practice: subjects అనే listలో రెండవ subject మరియు చివరి subjectను print చేయండి.
Hint: రెండవ item కోసం index 1ను, చివరి item కోసం negative index -1ను ఉపయోగించండి.
Practice: numbers = [10, 20, 30, 40, 50] listలో మొదటి number మరియు మూడవ numberను print చేయండి.
Hint: మొదటి itemకు index 0, మూడవ itemకు index 2 ఉపయోగించండి.
Common Mistakes
Pythonలో indexing 0తో ప్రారంభమవుతుంది. కాబట్టి మొదటి item కోసం index 0 ఉపయోగించాలి.
మూడు itemsకు valid indexes 0, 1, 2 మాత్రమే. List lengthకు సమానమైన index valid కాదు.
-1 చివరి item, -2 చివరి itemకు ముందు ఉన్న itemను సూచిస్తుందని గుర్తుంచుకోండి.
List indexing కోసం square brackets ఉపయోగించాలి: fruits[0].
Interview Questions
Python list indexing ఏ సంఖ్యతో ప్రారంభమవుతుంది?
Answer: Python list indexing 0తో ప్రారంభమవుతుంది.
Listలోని మొదటి itemను ఎలా access చేస్తారు?
Answer: మొదటి itemను access చేయడానికి list[0] ఉపయోగిస్తారు.
Listలోని చివరి itemను ఎలా access చేయవచ్చు?
Answer: చివరి itemను access చేయడానికి list[-1] ఉపయోగించవచ్చు.
Index 2 ఏ itemను సూచిస్తుంది?
Answer: Index 2 listలోని మూడవ itemను సూచిస్తుంది, ఎందుకంటే indexing 0తో ప్రారంభమవుతుంది.
Exam Questions
ఈ code output ఏమిటి? names = [“Ravi”, “Sita”, “Kiran”] ; print(names[1])
Answer:
Output Sita. Index 1 రెండవ itemను సూచిస్తుంది.
ఈ code output ఏమిటి? numbers = [5, 10, 15, 20] ; print(numbers[-1])
Answer:
Output 20. Negative index -1 చివరి itemను సూచిస్తుంది.
ఐదు items ఉన్న listలో valid positive indexes ఏవి?
Answer:
Valid positive indexes 0, 1, 2, 3, 4.
List indexing కోసం Pythonలో ఏ brackets ఉపయోగిస్తారు?
Answer:
Square brackets ఉపయోగిస్తారు. ఉదాహరణ: items[0].
Quick Revision
- List indexing ద్వారా listలోని ఒక itemను access చేయవచ్చు.
- Pythonలో list index 0తో ప్రారంభమవుతుంది.
- మొదటి itemకు index 0, రెండవ itemకు index 1 ఉంటుంది.
- చివరి itemను access చేయడానికి -1 ఉపయోగించవచ్చు.
- Indexing syntax: list_name[index].
- లేని indexను ఉపయోగిస్తే IndexError వస్తుంది.
- List indexingలో square brackets ఉపయోగించాలి.
