← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 8 of 15

Python zip() और enumerate()

zip() दो lists के items को zipper के दाँतों की तरह जोड़ता है, और enumerate() किसी list में जाते हुए साथ-साथ एक counter जोड़ता है, बिल्कुल लाइन में खड़े लोगों को नंबर देने जैसा। दोनों looping को आसान बनाते हैं।
Syntax
python
zip(iterable1, iterable2)
enumerate(iterable, start=0)

for index, item in enumerate(iterable):
    # body

zip() Function

zip(list1, list2, ...), कई iterables के corresponding elements को tuples में जोड़ता है, और सबसे छोटे input के खत्म होते ही रुक जाता है।

यह दो related lists, जैसे names और उनके corresponding scores, पर lockstep में loop करने का standard तरीका है, बिना दोनों में manually index किए।

उदाहरण: The zip() Function

python
names = ["Alex", "Sam"]
scores = [90, 80]
for name, score in zip(names, scores):  # pairs elements from both lists together
    print(name, score)

Lists को Unzip करना

एक और zip() call के अंदर किसी zipped result के आगे * लगाना उसे वापस अलग-अलग lists में unzip कर देता है, paired tuples को अलग-अलग कर देता है -- असल में zip() अपने ही operation को उलट देता है, यह एक genuinely उपयोगी लेकिन अक्सर नज़रअंदाज़ किया जाने वाला trick है।

उदाहरण: Unzipping Lists

python
pairs = [(1, "a"), (2, "b")]
numbers, letters = zip(*pairs)  # * unzips the pairs back into separate tuples
print(numbers, letters)

enumerate() Function

enumerate(iterable) किसी sequence को इस तरह wrap करता है कि उस पर loop करने से automatically (index, value) pairs मिलते हैं, जो हाथ से loop के अंदर बढ़ाए जाने वाले एक अलग counter variable को maintain करने वाले error-prone manual pattern की जगह लेता है।

उदाहरण: The enumerate() Function

python
fruits = ["apple", "banana"]
for index, fruit in enumerate(fruits):  # yields (index, value) pairs automatically
    print(index, fruit)

Custom Starting Index

enumerate(iterable, start=1) में एक start value पास करने से index count default 0 की बजाय उस number से शुरू होता है, जो तब उपयोगी है जब आप चाहते हैं कि 1-based numbering उसी तरह मैच करे जैसे output असल में किसी व्यक्ति को दिखाया जाएगा।

उदाहरण: Custom Starting Index

python
fruits = ["apple", "banana"]
for index, fruit in enumerate(fruits, start=1):  # index counting begins at 1 instead of 0
    print(index, fruit)

zip() और enumerate() को मिलाना

enumerate(zip(list1, list2)) की तरह enumerate() के अंदर zip() को nest करना आपको कई paired lists में loop करते हुए हर pair के लिए एक running index भी track करने देता है -- जब आपको position और paired values दोनों एक साथ चाहिए हों तो दोनों patterns को मिलाना।

उदाहरण: Combining zip() and enumerate()

python
names = ["Alex", "Sam"]
scores = [90, 80]
for i, (name, score) in enumerate(zip(names, scores)):  # index plus paired values together
    print(i, name, score)
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.