Python Nested Lists

Nested Lists అంటే ఏమిటి?

ఒక list లో మరో list ను ఉంచితే దాన్ని Nested List అంటారు. అంటే list లోని ప్రతి item ఒక సాధారణ value కావచ్చు లేదా మరో list కావచ్చు.

Nested Lists ఎందుకు ఉపయోగిస్తాము?

Rows మరియు columns రూపంలో ఉన్న data ను store చేయడానికి nested lists ఉపయోగిస్తాము. ఉదాహరణకు marks table, matrix, seating arrangement, shopping categories వంటి data ను సులభంగా నిర్వహించవచ్చు.

Nested List ఎలా పనిచేస్తుంది?

బయటి list లోని ప్రతి item ఒక inner list గా ఉంటుంది. మొదటి index తో row ను, రెండవ index తో ఆ row లోని value ను access చేయవచ్చు.

ఉదాహరణకు data[1][2] లో మొదటి 1 రెండవ inner list ను సూచిస్తుంది. రెండవ 2 ఆ inner list లోని మూడవ value ను సూచిస్తుంది. Python index counting zero నుంచి ప్రారంభమవుతుంది.

ఎక్కడ ఉపయోగిస్తారు?

  • Students marks table
  • Games board
  • Matrix data
  • Rows and columns data
  • Categories లోని items

Nested List ను create చేయడం

చదరపు brackets లోపల మరిన్ని lists రాయడం ద్వారా nested list ను create చేయవచ్చు.

marks = [[80, 75, 90], [65, 70, 85]]

ఇక్కడ marks లో రెండు inner lists ఉన్నాయి. మొదటి inner list ఒక student marks ను, రెండవ inner list మరో student marks ను సూచిస్తుంది.

Simple Explanation

Nested List అంటే list లో list. దీనిని table లాగా ఊహించుకోవచ్చు. బయట list rows ను సూచిస్తుంది. ప్రతి లోపలి list ఒక row లోని columns ను సూచిస్తుంది.

table[0] మొదటి row ను ఇస్తుంది. table[0][1] మొదటి row లోని రెండవ value ను ఇస్తుంది. Nested list లో value ను access చేయడానికి రెండు indexes ఉపయోగిస్తాము.

అన్ని values ను చూడడానికి సాధారణంగా nested for loops ఉపయోగిస్తాము. బయట loop ప్రతి row ను తీసుకుంటుంది. లోపలి loop ఆ row లోని ప్రతి value ను తీసుకుంటుంది.

Real-Life Example

ఒక classroom లో ఇద్దరు students మూడు subjects లో సాధించిన marks ను ఊహించండి. ప్రతి student marks ఒక row లాగా ఉంటుంది. అందువల్ల అన్ని marks ను ఒక nested list లో store చేయవచ్చు: మొదటి row మొదటి student marks, రెండవ row రెండవ student marks.

Code Examples

Nested List లో values ను access చేయడం మరియు print చేయడం

marks = [[80, 75, 90], [65, 70, 85]]

print(marks[0])
print(marks[1][2])

for row in marks:
    for mark in row:
        print(mark)

ఈ ఉదాహరణలో రెండు inner lists ఉన్న marks table ను create చేశాము. Indexes ఉపయోగించి ఒక row మరియు ఒక specific value ను access చేశాము. తరువాత nested for loops ద్వారా అన్ని marks ను ఒక్కొక్కటిగా print చేశాము.

[80, 75, 90]
85
80
75
90
65
70
85

Line-by-Line Explanation

marks = [[80, 75, 90], [65, 70, 85]]
ఈ line లో రెండు inner lists ఉన్న nested list ను create చేస్తున్నాము. ప్రతి inner list ఒక student యొక్క marks ను సూచిస్తుంది. Python ఈ మొత్తం data ను marks అనే variable లో store చేస్తుంది.
print(marks[0])
ఇక్కడ మొదటి inner list ను access చేస్తున్నాము. Index 0 మొదటి row ను సూచిస్తుంది. అందువల్ల మొదటి student marks పూర్తిగా output గా వస్తాయి.
print(marks[1][2])
మొదటి index 1 రెండవ inner list ను ఎంచుకుంటుంది. రెండవ index 2 ఆ list లోని మూడవ value ను ఎంచుకుంటుంది. కాబట్టి output గా 85 వస్తుంది.
for row in marks:
ఈ outer loop marks లోని ప్రతి inner list ను ఒక్కొక్కటిగా row variable లోకి తీసుకుంటుంది. మొదట మొదటి row, తరువాత రెండవ row వస్తాయి.
for mark in row:
ఈ inner loop ప్రస్తుత row లోని ప్రతి mark ను ఒక్కొక్కటిగా తీసుకుంటుంది. Outer loop ఒక row ను ఎంచుకున్న తరువాత, ఈ loop ఆ row లోని values ను process చేస్తుంది.
print(mark)
ప్రస్తుత mark value ను screen పై print చేస్తుంది. Inner loop పూర్తయ్యే వరకు ఆ row లోని అన్ని marks print అవుతాయి.

Try It

Try this Python code:

Example

students = [[70, 80], [90, 85]]

print(students[0])
print(students[1][0])

for row in students:
    for score in row:
        print(score)

Try It Yourself »

Practice

Practice: ఒక nested list prices = [[10, 20, 30], [40, 50, 60]] ను ఉపయోగించండి. రెండవ row ను print చేయండి, మొదటి row లోని మూడవ value ను print చేయండి, తరువాత nested loops ఉపయోగించి ప్రతి price ను ఒక్కొక్కటిగా print చేయండి.

Hint: రెండవ row కోసం index 1 ఉపయోగించండి. మొదటి row లోని మూడవ value కోసం prices[0][2] ఉపయోగించండి.

Practice: colors = [["red", "blue"], ["green", "yellow"]] అనే nested list లో రెండవ inner list లోని మొదటి value ను print చేయండి.

Hint: రెండవ list కోసం index 1, దాని మొదటి value కోసం index 0 ఉపయోగించండి.

Common Mistakes

ఒక nested list లో value ను access చేసేటప్పుడు ఒక index మాత్రమే ఉపయోగించడం. ఉదాహరణకు marks[1] తో specific value వస్తుందని అనుకోవడం.
ఒక inner list లోని specific value కోసం రెండు indexes ఉపయోగించాలి. ఉదాహరణకు రెండవ row లోని మూడవ value కోసం marks[1][2] వాడాలి.
Index counting ను 1 నుంచి ప్రారంభించడం.
Python indexing 0 నుంచి ప్రారంభమవుతుంది. మొదటి row index 0, రెండవ row index 1. అలాగే మొదటి value index 0.
Nested loop లో inner loop ను indentation లేకుండా రాయడం.
Inner loop, outer loop లోపల ఉండాలి. అందుకే inner loop line ముందు నాలుగు spaces indentation ఇవ్వాలి.
అన్ని rows లో values సంఖ్య ఒకేలా ఉంటాయని తప్పుగా అనుకోవడం.
ప్రతి inner list లో values సంఖ్య వేరుగా ఉండవచ్చు. Access చేసే ముందు ఆ row లో ఆ index ఉందో లేదో చూసుకోవాలి.

Interview Questions

Nested List అంటే ఏమిటి?

Answer: ఒక list లో మరో list ఉండే structure ను Nested List అంటారు. ఇది table లేదా rows and columns data ను store చేయడానికి ఉపయోగపడుతుంది.

Nested List లో ఒక specific value ను ఎలా access చేస్తారు?

Answer: రెండు indexes ఉపయోగిస్తారు. మొదటి index inner list లేదా row ను, రెండవ index ఆ row లోని value ను సూచిస్తుంది. ఉదాహరణకు data[1][2].

Nested List లో nested loops ఎందుకు ఉపయోగిస్తారు?

Answer: మొదటి loop ప్రతి inner list ను తీసుకుంటుంది. రెండవ loop ఆ inner list లోని ప్రతి value ను తీసుకుంటుంది. అందువల్ల nested list లోని అన్ని values ను process చేయవచ్చు.

Exam Questions

ఈ code యొక్క output ఏమిటి? numbers = [[1, 2], [3, 4]] మరియు print(numbers[1][0]).

Answer:

Output 3. మొదటి index 1 రెండవ inner list ను, రెండవ index 0 ఆ list లోని మొదటి value ను సూచిస్తుంది.

Nested List ను create చేసే Python statement రాయండి.

Answer:

table = [[1, 2], [3, 4]]

Nested List లో మొదటి row ను access చేయడానికి ఏ expression ఉపయోగిస్తారు?

Answer:

మొదటి row కోసం table[0] ఉపయోగిస్తారు.

Quick Revision

  • Nested List అంటే list లో list.
  • Nested Lists ను rows and columns data కోసం ఉపయోగిస్తాము.
  • మొదటి index inner list లేదా row ను సూచిస్తుంది.
  • రెండవ index ఆ row లోని value ను సూచిస్తుంది.
  • Python indexing 0 నుంచి ప్రారంభమవుతుంది.
  • అన్ని values ను access చేయడానికి nested for loops ఉపయోగించవచ్చు.
  • Inner loop కు సరైన indentation తప్పనిసరి.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top