Python Lists Part 3, Everyday Techniques in English & Hindi

Practical Python Lists

Copying, Nesting, and String Manipulation

In our previous posts, we mastered the basics of lists. Now, let's cover some essential techniques you'll use every day.

1. Copying a List (The Right Way!)

This is very important! If you use `=`, you're not making a copy; both variables will point to the same list. Changing one will change the other.

# Wrong way - This is NOT a copy
original = [1, 2, 3]
reference = original
reference.append(4)

print("Reference:", reference) # Output: [1, 2, 3, 4]
print("Original:", original)   # Output: [1, 2, 3, 4] <-- also="" changed="" is="" original="" pre="">
                    
                    

To create a true, independent copy, use the `.copy()` method or a full slice `[:]`.

# Correct way - This IS a copy
original = [1, 2, 3]
the_copy = original.copy()
the_copy.append(4)

print("The Copy:", the_copy) # Output: [1, 2, 3, 4]
print("Original:", original) # Output: [1, 2, 3] <-- is="" original="" pre="" unchanged="">
                

2. Nested Lists (Lists within Lists)

A nested list is a list that contains other lists as its elements. This is great for creating a matrix or a grid.

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# To access an element, use two sets of brackets: [row][column]
# Access the number 5 (row 1, column 1)
element = matrix[1][1] 
print(element) # Output: 5

3. Converting Between Strings and Lists

These are incredibly useful string methods that work directly with lists.

`.split()` - String to List:

Splits a string into a list of substrings based on a separator.

sentence = "hello world welcome to python"
words = sentence.split(" ")
print(words) # Output: ['hello', 'world', 'welcome', 'to', 'python']
`.join()` - List to String:

Joins the elements of a list into a single string, with a specified separator between them.

words = ['Python', 'is', 'fun']
sentence = " ".join(words)
print(sentence) # Output: "Python is fun"
पिछली पोस्ट्स में, हमने लिस्ट्स की मूल बातें सीखीं। अब, आइए कुछ आवश्यक तकनीकों को कवर करें जिनका आप हर दिन उपयोग करेंगे।

1. लिस्ट को कॉपी करना (सही तरीका!)

यह बहुत महत्वपूर्ण है! यदि आप `=` का उपयोग करते हैं, तो आप एक कॉपी नहीं बना रहे हैं; दोनों वेरिएबल्स एक ही लिस्ट को इंगित करेंगे। एक को बदलने से दूसरा भी बदल जाएगा।

# गलत तरीका - यह एक कॉपी नहीं है
original = [1, 2, 3]
reference = original
reference.append(4)

print("रेफरेंस:", reference) # आउटपुट: [1, 2, 3, 4]
print("ओरिजिनल:", original)   # आउटपुट: [1, 2, 3, 4] <-- pre="">
                    
                    

एक वास्तविक, स्वतंत्र कॉपी बनाने के लिए, `.copy()` मेथड या एक पूर्ण स्लाइस `[:]` का उपयोग करें।

# सही तरीका - यह एक कॉपी है
original = [1, 2, 3]
the_copy = original.copy()
the_copy.append(4)

print("कॉपी:", the_copy) # आउटपुट: [1, 2, 3, 4]
print("ओरिजिनल:", original) # आउटपुट: [1, 2, 3] <-- pre="">
                

2. नेस्टेड लिस्ट्स (लिस्ट्स के अंदर लिस्ट्स)

एक नेस्टेड लिस्ट वह लिस्ट होती है जिसके एलिमेंट्स के रूप में अन्य लिस्ट्स होती हैं। यह मैट्रिक्स या ग्रिड बनाने के लिए बहुत अच्छा है।

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# किसी एलिमेंट तक पहुंचने के लिए, दो ब्रैकेट सेट का उपयोग करें: [पंक्ति][कॉलम]
# संख्या 5 तक पहुंचें (पंक्ति 1, कॉलम 1)
element = matrix[1][1] 
print(element) # आउटपुट: 5

3. स्ट्रिंग्स और लिस्ट्स के बीच रूपांतरण

ये अविश्वसनीय रूप से उपयोगी स्ट्रिंग मेथड्स हैं जो सीधे लिस्ट्स के साथ काम करते हैं।

`.split()` - स्ट्रिंग से लिस्ट:

एक स्ट्रिंग को एक सेपरेटर के आधार पर सबस्ट्रिंग्स की लिस्ट में विभाजित करता है।

sentence = "नमस्ते दुनिया पाइथन में आपका स्वागत है"
words = sentence.split(" ")
print(words) # आउटपुट: ['नमस्ते', 'दुनिया', 'पाइथन', 'में', 'आपका', 'स्वागत', 'है']
`.join()` - लिस्ट से स्ट्रिंग:

एक लिस्ट के एलिमेंट्स को एक ही स्ट्रिंग में जोड़ता है, जिसमें उनके बीच एक निर्दिष्ट सेपरेटर होता है।

words = ['पाइथन', 'मजेदार', 'है']
sentence = " ".join(words)
print(sentence) # आउटपुट: "पाइथन मजेदार है"

Comments