← Back to Python Course | Chapter 6: Data Structures | Lesson 1 of 12

Python Lists

List एक shopping list की तरह है जिसमें items एक क्रम में रखे जाते हैं। आप किसी भी item को उसकी position से देख सकते हैं, बदल सकते हैं, या सबको एक-एक करके देख सकते हैं।
Syntax
python
list_name = [item1, item2, item3]
list_name[index]

List क्या है?

List items का एक ordered sequence store करता है जिसे बनाने के बाद बदला जा सकता है, और कई अन्य languages के arrays के विपरीत यह एक ही list में अलग-अलग types (numbers, strings, यहाँ तक कि दूसरी lists भी) को स्वतंत्र रूप से mix कर सकता है।

यह वह default general-purpose container है जिसे आप Python में सबसे पहले इस्तेमाल करते हैं।

उदाहरण: What is a List?

python
mixed = [1, "two", 3.0, [4]]
print(mixed)

List Items को Access करना

Square-bracket indexing पहले element के लिए 0 से शुरू होती है, और negative indices अंत से पीछे की ओर गिनती करते हैं ताकि my_list[-1] हमेशा list की length जाने बिना आखिरी item दे दे।

उदाहरण: Accessing List Items

python
items = [10, 20, 30]
print(items[0])  # first item, index 0
print(items[-1])  # last item, negative index counts from the end

List Slicing

list[start:end] से slicing करने पर original को बदले बिना एक sub-list निकल आती है, और चूँकि end index exclusive होता है, list[0:3] ठीक तीन elements देता है — यह एक ऐसी बात है जो inclusive ranges वाली languages से आने वाले beginners को confuse कर देती है।

उदाहरण: List Slicing

python
items = [10, 20, 30, 40, 50]
print(items[0:3])

List Items को Modify करना

चूँकि lists mutable होती हैं, आप my_list[i] = new_value दोबारा assign करके एक नया list object बनाए बिना किसी element को in place बदल सकते हैं, जो performance के लिए मायने रखता है और इसका मतलब यह भी है कि एक ही list को इशारा करने वाले दो variables दोनों में यह बदलाव दिखेगा।

उदाहरण: Modifying List Items

python
items = [10, 20, 30]
items[1] = 99  # reassigns the element at index 1 in place
print(items)

Lists पर Iterate करना

List पर for loop हर element को सीधे उसके क्रम में देता है (index नहीं), जो items को process करने का idiomatic तरीका है — enumerate() का इस्तेमाल तभी कीजिए जब आपको हर item की position भी चाहिए हो।

उदाहरण: Iterating over Lists

python
items = [10, 20, 30]
for item in items:  # yields each value directly, not the index
    print(item)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.