अभी जो भी variables python में initialize कर रहे थे वो सिर्फ single value को ही store कर रहे थे। लेकिन अगर हमें single variable में multiple values store करनी हो हो ? इस problem को solve किया है list ने।


list python में defined 4 built in data types (list , tuple , set , dictionary) में से एक है जिसका use data collections को store करने किया जाता है। data collections means एक values का collection.


list data items को order में store करता है , duplicate items को allow करता है और वो items changeable भी हैं।

  1. Ordered - ordered मतलब जिस order में हमने items को list में insert किया था हमें उसी order में मिलेंगे।

  2. Duplicate - duplicate means हम same item को कई बार insert कर सकते हैं।

  3. Changeable - मतलब उन items को जरूरत पड़ने पर update कर सकते हैं या list में नए items को add कर सकते हैं।


Python में list हम square brackets [ ] use करके create कर सकते हैं -

#using square brackets. 
list_var = [ item1,item2.....itemn ]

Python List Example

Copy Fullscreen Close Fullscreen Run
list_var = [12,45,56,5,76,78,78,45,12]
print(list_var)
Output
C:\Users\Rahulkumar\Desktop\python>python list_ex.py
[12, 45, 56, 5, 76, 78, 78, 45, 12]

Note : यह जरूरी नहीं है कि list में सिर्फ same type ही items हों , आप अपनी need के according किसी भी type (String , Boolean , Numeric) के elements insert कर सकते हैं।
For Example - list_var = ["name", 34, 56.78, Tru]

Python Iterating list

हालाँकि list items indexed होते हैं। मतलब हर item का index number होता है जो कि 0 से start होता है। n numbers के items की list के लिए maximum index number n - 1 ही होगा। Index होने की वजह से ही इन्हे हैं for loop / while loop की help से iterate कर सकते हैं।
For Example -

Copy Fullscreen Close Fullscreen Run
list_var = [12,45,56,5,76,78,78,45,12]
for item in list_var :
    print(item)
Output
C:\Users\Rahulkumar\Desktop\python>python list_ex.py
12
45
56
5
76
78
78
45
12

तो कुछ इस तरह से list को iterate करते हैं।

Python list length & type

list की length जानने के लिए len() function का use किया जाता है और , type जानने के लिए type() function का use किया जाता है।

Copy Fullscreen Close Fullscreen Run
list_var = [12,45,56,5,76,78,78,45,12]
print('list length:', len(list_var))
print('type :', type(list_var))
Output
C:\Users\Rahulkumar\Desktop\python>python list_ex.py
list length: 9
type : 

Python access single item from list

चूंकि items index number से associate होते हैं इसलिए हम direct index number से भी items को access कर सकते हैं।
For Example -

#accessing 2nd item
print(list_var[1]) #O/P : 45

#accessing 5nd item
print(list_var[4]) #O/P : 76

colon : का use करके हम एक particular list का part भी access कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
list_var = [12,45,56,5,76,78,78,45,12]
print(list_var[2:])  #all items from 3rd index.
print(list_var[4:6])  #all items from 4th to 6th index (6th index item को छोड़कर) .
print(list_var[:5])  #all items from starting to 5th index. 
print(list_var[-1])  #last item. 
print(list_var[3:-1])  #all items from 3rd index to n-1 th index (-1th index item को छोड़कर) .
print(list_var[-8:-1])  #all items from 8th index from last to -1 index from last (-1th index item को छोड़कर).
Output
C:\Users\Rahulkumar\Desktop\python>python list_ex.py
[56, 5, 76, 78, 78, 45, 12]
[76, 78]
[12, 45, 56, 5, 76]
12
[5, 76, 78, 78, 45]
[45, 56, 5, 76, 78, 78, 45]

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers