Lists
- List एक ऐसा Data Structure है जिसमें एक से अधिक डेटा (Items) को क्रम (Order) में रखा जाता है।
- List data types में एक से ज्यादा items होते हैं |
- List में हर items को comma ( , ) से separate किया जाता है और list के सभी items को square brackets के अंदर close किया जाता है |
- List एक compound data types है जिसमें किसी भी data types के items लिए जा सकते हैं list यह एक mutable data types है इन data types के items की value change की जा सकती है |
- List का पहला Index हमेशा 0 से शुरू होता है।
Creating List in Python
List के semicolon (:) दे या ना दे इससे कोई फर्क नहीं पड़ता है |
Source Code :
list1 = [1, 2, 3, 4, 5] #Integer List
list2 = [1.5, 2.4, 3.8, 4.4, 5.7]; #Float List
list3 = [1, 2, "Hello", 4.5]; #Mixed Data Types List
list4 = [1, 2, [1, 2], 4.5] #Nested List
Positive and Negative Indexing
1. List Positive Indexing
जब List के Elements को बाएँ (Left) से दाएँ (Right) Access किया जाता है, तो उसे Positive Indexing कहते हैं।
इसमें Index हमेशा 0 से शुरू होता है।
Source Code :
strList = ["H", "e", "l", "l", "o"]
Indexing Table :
| Positive Index | Value | Negative Index |
|---|---|---|
| 0 | H | -5 |
| 1 | e | -4 |
| 2 | l | -3 |
| 3 | l | -2 |
| 4 | o | -1 |
2. Negative Indexing
जब List के Elements को दाएँ (Right) से बाएँ (Left) Access किया जाता है, तो उसे Negative Indexing कहते हैं।
इसमें Index हमेशा -1 से शुरू होता है।
Source Code :
strList = ["H", "e", "l", "l", "o"]
Indexing table :
| Negative Index | Value |
|---|---|
| -5 | H |
| -4 | e |
| -3 | l |
| -2 | l |
| -1 | o |
3. Accessing List
जब हम किसी List के किसी विशेष Element को उसकी Index Position की सहायता से प्राप्त करते हैं, तो उसे Accessing List कहते हैं।
Python में Indexing दो प्रकार की होती है।
- Positive Indexing
- Negative Indexing
Source Code :
list = [1, 2, "H", 2.8]
print(list[0])
print(list[1])
print(list[2])
print(list[3])
Output :
1
2
H
2.8
4. invalid Indexing
यदि हम ऐसी Index Position को Access करें जो List में मौजूद ही नहीं है, तो Python Error देता है।
इसे Index Error कहते हैं।
Source Code :
list = [1, 2, "H", 2.8]
print(list[10])
Output :
print(list[10])
IndexError: list index out of range
5. Accessing Nested List
यदि किसी List के अंदर दूसरी List हो, तो उसे Nested List कहते हैं।
Source Code :
list = [1, 2, "Hello", ["R", "U"]]
print(list[3][0]) #Output : R
print(list[3][1]) #Output : U
6. Convert list to sub-list or list slicing
List के किसी भाग को कहां से शुरू करना है और कहां तक जाना है यह निकालने के लिए list slicing का उपयोग किया जाता है इस list slicing कहते हैं |
Syntax :
list[start:end]
Source code :
list = [1, 2, "H", 2.8]
print(list[0:1]) # [1]
print(list[0:2]) # [1, 2]
print(list[1:3]) # [2, 'H', 2.8]
print(list[3:1]) #[]
Output :
[1]
[1, 2]
[2, ‘H’, 2.8]
[]
अगर colon (:) के left side का index invalid होता है तो blank list ([]) return होती है और colon (:) के right side का index invalid होता है | तो list के left index से आखरी तक index return होता है अगर दोनों ही invalid index होता है तो blank list ([]) return होता है |
Example :
list = [1, 2, "H", 2.8]
print(list[0:1]) # [1]
print(list[0:2]) # [1, 2]
print(list[1:3]) # [2, 'H']
print(list[1:15]) # [2, 'H', 2.8]
print(list[10:4]) # []
7. Check Length of List’s items
List में कुल कितने Elements हैं, यह जानने के लिए len() Function का उपयोग किया जाता है।
Source Code :
list = [1, 2, "H", 2.8]
print (len(list))
#Output : 4
8. List Concatenation
दो या दो से अधिक Lists को जोड़ना Concatenation कहलाता है।
Source Code :
list = [1, 2, "H", 2.8] + ["Hello", [2, 8]]
print (list)
#Output : [1, 2, 'H', 2.8, 'Hello', [2, 8]]
9. Iterating List items using for loop
List के प्रत्येक Element को एक-एक करके पढ़ने (Traverse करने) की प्रक्रिया Iteration कहलाती है।
Python में इसके लिए सबसे अधिक for Loop का उपयोग किया जाता है।
Source Code :
list = [1, 2, "H", 2.8]
for i in list:
print(i)
#Output :
1
2
H
2.8
10. List repetition
एक ही List को कई बार दोहराने के लिए * Operator का उपयोग किया जाता है।
Source Code :
list = [1, 2, "H", 2.8] * 3
print (list)
#Output : [1, 2, 'H', 2.8, 1, 2, 'H', 2.8, 1, 2, 'H', 2.8]
