List Overview

Python me kuch data types hain jo programming ko bhut hi easy, fast aur robust banate hain. Dekha jaye to isme four important data types jaise List, Dictionary, Sets aur Tuples, jo mostly Python programming me use kiye jate hain. 

Python me List kya hai?

List data ke collections ko store karta hai aur important factor yah hai ki ye har ek types ke data ko store kar sakta hai. For example – 
list_data = [“hello”, “Python”,2020]

upar diye gaye example me list_data ek list variable ha jaha hum string aur integer dono tarah ki values ko store kar sakte hain. 

List ki khasiyat yah hai ki ye mutable hoti hai, Mutable means – List ki size aur usme present data ko change kiya ja sakta hai. Kisi bhi list me data hamesha square bracket ‘ [ ] ‘ ke andar store hote hain aur jise hum indexing ke through execute bhi kar sakte hain. jaise :
list_data[0] = hello
list_data[1] = Python
list_data[2] = 2020

Python me List ko List Constructor ka use karke bhi define kiya ja sakta hai.
Ex : list_data = list((“hello”, “Python”,2020))
       
print(list_data)

*** list(( … )) – Double parenthesis ka use start our end me use karna compulsory hota hai.

A Simple Example of List: (Positive Indexing)

listvalue = [“america”“india”2020]
print(listvalue)
type(listvalue)
type(listvalue[0])
type(listvalue[1])
type(listvalue[2])

List - Positive aur Negative Indexing

Humne upar jis example ko discuss kiya hai wah ek positive indexing hai, jaha hum  listvalue[0] , listvalue[1] , listvalue[2] ka use karke list ki values ko access kar pa rahe hain. 

Negative Indexing kuch aisa hi hai jaha hum kisi bhi index ko ‘ ‘ sign ke sath use karke use access kar payenge. Samajhne ke liye niche diye gaye example ko dekhiye… 

List – Negative Indexing:

listvalue = [“america”“india”2020]
print(listvalue)
type(listvalue)
type(listvalue[-1])
type(listvalue[-2])
type(listvalue[-3])

List me Index Range kya hota hai?

List me Index Range ko samajhne ke liye hum ek example dekhten hain : 

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
print(listval)
print(listval[:])
print(listval[0:])
print(listval[2:4])
print(listval[2:])
print(listval[-5:-2])
print(listval[0:6:1])

Example ko dekhkar aap samajh pa rahen honge ki hum kaise range of values ko access kar rahe hain. 
listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
                 [0]     [1]       [2]               [3]              [4]
                [-5]    [-4]      [-3]              [-2]             [-1]

listval[2:4] me ‘2’ start index aur ‘4’ end index hai. jisko run karne par aap uss bich ke range ki values ko dekh payenge.

Isi tarah listval[0:6:1] me ‘0‘ start index aur ‘6’ end index hai lekin yaha ‘1’ ek step index hai jiska matlab yah hai ki output me aap 1 value skip kar rahen hain…means aap [0], [2],[4] index ki value dekh payenge.

 

List me values ko change karen

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
print(listval)
listval[4] = “Srilanka”
print(listval)

Python list me kisi bhi index ko outside se change kiya ja sakta hai. Jaisa ki example se samajh me aa raha h ki hum 4th index ki value ko change kar rahen hain.

List ki Length calculate karen

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
print(len(listval))

Kisi bhi list ki length len() function ka use karke nikala ja sakta hai.

List me value add karen

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
print(listval)
listval.append(“London”)
listval.insert(4,”Japan”)

List me values add karne ke liye hum append() aur insert() function ka use karte hain. Agar append() ka use hota hai to iska matlab yah hai ki wah value kisi bhi list ke end me add hogi, lekin agar hum chahte hain ki use particular index me add kiya jaye to hum insert() function ka use karenge, jaisa ki aap upar diye gaye example me dekh pa rahen honge ki hum 4th index me ‘Japan’ add kar rahen hain.

List me value remove karen

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
print(listval)
listval.pop()
listval.remove(“Canada”)
del.listval[3]
del.listval
listval.clear()

List me kuch functions jaise pop(), remove(), del(), clear() ka use hota hai.chaliye one by one unke functionalities ko samajhte hain. 

pop() function append() function ka opposite hai means jis tarah append function list ke end me values add karta hai thi usi tarah pop() function list ke ends se values ko remove karta hai.

remove() function bhi data ko erase karne ka kam karta hai, lekin isse use karte samay isme ek argument pass karna hota hai, jaisa ki example me show kiya gaya hai.

del() function list ke kisi particular index ki value ko ya fir poori list ko hi delete karne ki capacity rakhta hai.

clear() function list ko empty karta hai. Yaad rahe del() aur clear() function me yah difference hai ki del() function poori tarah list ki presence ko delete kar dega lekin clear() function list ko sirf empty karega. 

List me looping ka concept

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]

for x in listval:
          print (x)

Python me loop ka concept bahut hi easy hai. Jaisa ki example me show ho raha hai ki “for loop” automatically x ka starting index assign kar raha hai, jaha list 0th index se shuru ho raha hai. yah pahle 0th index ki value print karega aur backend me index ki value increment kar dega aur fir wah 1st index ki value print karega. Issi tarah list ke last index tak output execute hota jayega.

Python List me Copy function kaise kam karta hai ?

listval=[“usa”,”india”,”England”,”Canada”,”Singapore”]
listval2 = listval.copy()
print(listval)
print(listval2)

Python list me agar hum copy fuction ka use karna chahte hain to hum usse directly assign nhi kar sakte , jaise listval = listval2 kyuki yaha hum listval ka reference listval2 me assign kar rahe hain jiska matlab yah hai ki agar hum listval2 me kuch changes kar rahen hain to wah listval me bhi reflect hoga jo code me problem create kar sakta hai. 

Issliye hum listval2 ko listval.copy() se assign karenge. Jisse listval2 ko alag se address assign ho jayega aur lisval se poori tarah seperate rakhega.

Lists ko aapas me kaise add kiya jata hai?

listval1=[“usa”,”india”,”England”,”Canada”]
listval2=[“Singapore”,”Belizium”]
listval3 = listval1 + listval2
print(listval3)

listval1 = [“india”, “bhutan” , “china”]
listval2 = [“srilanka”, “bangladesh”]

listval1.extend(listval2)
print(listval1)

listval1 = [“india”, “bhutan” , “china”]
listval2 = [“srilanka”, “bangladesh”]

for a in listval2 :
    listval1.append(a)

print(listval1)

List ko apas me add karne ke liye hum ‘+’ operator, append() aur extend() function ka use kar sakte hain.