Updating List Items

Updating List Items అంటే ఏమిటి?

List లో ఇప్పటికే ఉన్న item విలువను మార్చడాన్ని Updating List Items అంటారు. Python list లోని item ను దాని index ఉపయోగించి కొత్త విలువతో update చేయవచ్చు.

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

ఒక list లో ఉన్న సమాచారం మారినప్పుడు మొత్తం list ను మళ్లీ తయారు చేయాల్సిన అవసరం లేదు. అవసరమైన item ను మాత్రమే మార్చవచ్చు.

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

ముందుగా list లోని item యొక్క index ను గుర్తించాలి. తరువాత ఆ index కు కొత్త విలువను assign చేయాలి.

ఉదాహరణకు, items[1] = new_value అని రాస్తే, index 1 వద్ద ఉన్న item కొత్త విలువతో update అవుతుంది.

ఎక్కడ ఉపయోగపడుతుంది?

  • Student marks మార్చడానికి
  • Shopping cart లో product quantity లేదా పేరు update చేయడానికి
  • Game scores మార్చడానికి
  • Application settings లేదా user information update చేయడానికి

ముఖ్యమైన విషయం

Python list index 0 నుండి ప్రారంభమవుతుంది. కాబట్టి మొదటి item ను update చేయడానికి index 0 ఉపయోగించాలి.

Simple Explanation

List ను ఒక వరుసలో ఉన్న boxes లాగా ఊహించండి. ప్రతి box కు ఒక index ఉంటుంది. ఒక box లో ఉన్న value మార్చాలంటే, ఆ box యొక్క index ను ఉపయోగించి కొత్త value assign చేయాలి.

ఉదాహరణ: colors[0] = "yellow" అంటే colors list లోని మొదటి item ను yellow గా మార్చడం.

List mutable కాబట్టి, list తయారైన తర్వాత కూడా దాని items ను మార్చవచ్చు.

Real-Life Example

ఒక shopping list లో milk స్థానంలో juice కొనాలని నిర్ణయించుకున్నారని అనుకోండి. మొత్తం shopping list ను తొలగించకుండా, milk ఉన్న item ను మాత్రమే juice గా మార్చవచ్చు. ఇదే Python list item update చేయడం.

Code Examples

ఒక List Item ను Update చేయడం

fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits)

ఈ code లో index 1 వద్ద ఉన్న banana ను mango గా మార్చాము. మిగిలిన items అలాగే ఉంటాయి.

['apple', 'mango', 'cherry']

List Slicing ఉపయోగించి కొన్ని Items ను Update చేయడం

numbers = [10, 20, 30, 40]
numbers[1:3] = [200, 300]
print(numbers)

ఇక్కడ index 1 నుండి index 2 వరకు ఉన్న items ఎంపిక అవుతాయి. వాటి స్థానంలో 200, 300 values వస్తాయి.

[10, 200, 300, 40]

Line-by-Line Explanation

fruits = [“apple”, “banana”, “cherry”]
ఈ line లో మూడు items ఉన్న fruits అనే list తయారవుతుంది. List లో apple index 0, banana index 1, cherry index 2 వద్ద ఉంటాయి.
fruits[1] = “mango”
ఈ line index 1 వద్ద ఉన్న banana item ను mango తో replace చేస్తుంది. Python list లోని పాత value తొలగిపోయి కొత్త value అక్కడ ఉంచబడుతుంది.
print(fruits)
ఈ line update అయిన మొత్తం fruits list ను screen పై చూపిస్తుంది.
numbers = [10, 20, 30, 40]
ఈ line నాలుగు number items ఉన్న numbers list ను తయారు చేస్తుంది.
numbers[1:3] = [200, 300]
ఇక్కడ slicing ద్వారా index 1 మరియు index 2 items ఎంపిక అవుతాయి. వాటి స్థానంలో 200 మరియు 300 values update అవుతాయి.
print(numbers)
ఈ line update అయిన numbers list ను output గా చూపిస్తుంది.

Try It

Try this Python code:

Example

colors = ["red", "blue", "green"]
colors[0] = "yellow"
print(colors)

Try It Yourself »

Practice

Practice: marks అనే list లో [60, 75, 90] values ఉన్నాయి. రెండవ mark ను 85 గా update చేసి list ను print చేయండి.

Hint: రెండవ item యొక్క index 1. ఆ index కు 85 assign చేయండి.

Practice: cities అనే list లో [“Hyderabad”, “Chennai”, “Delhi”] values ఉన్నాయి. Chennai ను Mumbai గా update చేసి list ను print చేయండి.

Hint: Chennai index 1 వద్ద ఉంది.

Practice: numbers అనే list లో [1, 2, 3, 4] values ఉన్నాయి. index 0 item ను 10 గా update చేయండి.

Hint: మొదటి item కోసం index 0 ఉపయోగించాలి.

Common Mistakes

మొదటి item కోసం index 1 ఉపయోగించడం.
Python indexing 0 నుండి ప్రారంభమవుతుంది. కాబట్టి మొదటి item index 0, రెండవ item index 1.
Update చేయడానికి == ఉపయోగించడం, ఉదాహరణకు fruits[1] == “mango”.
Value assign చేయడానికి single equals sign = ఉపయోగించాలి. సరైన code: fruits[1] = “mango”.
List లో లేని index ను update చేయడం.
IndexError రాకుండా list లో ఉన్న valid index మాత్రమే ఉపయోగించాలి.
Index ను quotes లో రాయడం, ఉదాహరణకు colors[“0”] = “yellow”.
List index సాధారణంగా integer గా ఉండాలి. సరైన విధానం colors[0] = “yellow”.

Interview Questions

Python list లో item ను ఎలా update చేస్తారు?

Answer: Item యొక్క index ను ఉపయోగించి కొత్త value assign చేస్తారు. ఉదాహరణ: items[0] = “new_value”.

Python list లో indexing ఏ సంఖ్య నుండి ప్రారంభమవుతుంది?

Answer: Python list indexing 0 నుండి ప్రారంభమవుతుంది.

Python lists mutable అంటే ఏమిటి?

Answer: List తయారైన తర్వాత కూడా దాని items ను మార్చవచ్చు. అందుకే lists ను mutable అంటారు.

List లోని రెండవ item ను update చేయడానికి ఏ index ఉపయోగిస్తారు?

Answer: రెండవ item కోసం index 1 ఉపయోగిస్తారు.

Exam Questions

ఈ code యొక్క output ఏమిటి? values = [5, 10, 15]; values[0] = 50; print(values)

Answer:

[50, 10, 15]

List లోని మూడవ item ను update చేయడానికి ఏ index ఉపయోగించాలి?

Answer:

Index 2 ఉపయోగించాలి.

ఈ statement ను పూర్తి చేయండి: names[1] ___ “Ravi”

Answer:

= ఉపయోగించాలి. పూర్తి statement: names[1] = “Ravi”.

ఒక list లో లేని index కు value assign చేస్తే సాధారణంగా ఏ error వస్తుంది?

Answer:

IndexError వస్తుంది.

Quick Revision

  • List item update చేయడానికి index ను ఉపయోగించాలి.
  • Python list indexing 0 నుండి ప్రారంభమవుతుంది.
  • సరైన syntax: list_name[index] = new_value.
  • మొదటి item index 0, రెండవ item index 1.
  • Lists mutable కాబట్టి వాటి items ను మార్చవచ్చు.
  • ఒకేసారి కొన్ని items ను update చేయడానికి slicing assignment ఉపయోగించవచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top