Python List

Python List

Lists: List is ordered collection in Python. If we want to represent a group of individual objects as a single entity where insertion order preserved and duplicates are allowed, then we should go for list. Lists are related to arrays of programming languages like C, C++ or Java, but Python lists are by far more flexible and powerful than “classical” arrays.

पायथन में लिस्ट एक आदेशों का संग्रह होता है। यदि हम एक एकल इकाई के रूप में अलग-अलग वस्तुओं के एक समूह का प्रतिनिधित्व करना चाहते हैं, जहां सम्मिलन क्रम संरक्षित है और डुप्लिकेट की अनुमति है, तो हमें लिस्ट में जाना चाहिए। सूचियाँ C, C++ या Java जैसी प्रोग्रामिंग भाषाओं के टेबल से संबंधित हैं, लेकिन पायथन लिस्ट “classical” ऐरे से कहीं अधिक लचीली और शक्तिशाली हैं।

The list type is essential for Python scripts and programs; this means that you will hardly find any serious Python code without a list.

पायथन स्क्रिप्ट और प्रोग्रामों के लिए लिस्ट टाइप आवश्यक है, इसका मतलब है कि आपको बिना किसी लिस्ट के शायद ही कोई सीरियस पायथन कोड मिलेगा।

The main properties of Python lists:
पायथन लिस्ट के मुख्य प्रापर्टीज

  • Insertion order is preserved

इंसर्शन आर्डर संरक्षित है .

  • Duplicate values are allowed

डुप्लिकेट मानों की अनुमति है

  • Heterogeneous objects are allowed (It can be an arbitrary mixture of elements like numbers, strings, and other lists and so on.)

विषम ऑब्जेक्ट की अनुमति है (यह संख्या, स्ट्रिंग्स और अन्य सूचियों और इसी तरह के तत्वों का एक आर्बिट्रेरी मिक्सचर हो सकता है।)

  • Elements of a list can be accessed by an index

एक लिस्ट के एलीमेंट पर एक इंडेक्स द्वारा पहुंचा जा सकता है।

  • We can differentiate duplicate elements by using index and we can preserve insertion order by using index. Hence index will play important role.

हम इंडेक्स का उपयोग करके डुप्लिकेट एलीमेंट को अलग कर सकते हैं और इंडेक्स का उपयोग करके इंसर्शन आर्डर को संरक्षित कर सकते हैं। इसलिए इंडेक्स महत्वपूर्ण भूमिका निभाएगा।

  • List is dynamic because based on our requirement we are increase the size or decrease the size.

लिस्ट डॉयनेमिक है क्योंकि हमारी आवश्यकता के आधार पर हम आकार में वृद्धि कर सकते हैं या आकार में कमी कर सकते हैं।

  • List objects are mutable i.e., we can change the content. लिस्ट ऑब्जेक्ट परस्पर हैं यानी हम कंटेंट को बदल सकते हैं। In list the elements will be placed with square brackets ([]) and with comma (,) separator.

सूची में एलीमेंट को स्क्वैयर ब्रैकेट ([]) और अल्पविराम () विभाजक के साथ रखा जाएगा।

Creation of the list objects:
लिस्ट ऑब्जेक्ट्स का निर्माण

You can create a list objects in Python programming through various methods given below. You will see that a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas they may be of different types (integer, float, string etc.).

आप नीचे दिए गए विभिन्न विधिओ के माध्यम से पायथन प्रोग्रामिंग में एक लिस्ट ऑब्जेक्ट बना सकते हैं। आप देखेंगे कि एक लिस्ट एक वर्ग कोष्ठक के अंदर सभी वस्तुओं (तत्वों) को रखकर बनाई गई है [], अल्पविराम द्वारा अलग किए गए वे विभिन्न प्रकार (integer, float, string आदि) के हो सकते हैं।

1.We can create empty list object as follows
 हम निम्न प्रकार से इम्पटी लिस्ट ऑब्जेक्ट बना सकते हैं।
>>> list = []
>>> print (list)
[]
>>> print (type (list))
<class ‘list’>

2.Create list with elements
एलीमेंट के साथ लिस्ट का निर्माण
>>> list= [10, 20,30,40]
>>> print (list)
[10, 20, 30, 40]
>>> list= [ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> print (list)
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> list= [True, False]
>>> print (list)
[True, False]

3. Create list with dynamic inputs
डॉयनॉमिक इनपुट के साथ लिस्ट बनाएं
# Test.py
list= eval(input (“Enter List :”))
print (list)

Output:
Enter List: [10, 20, 30, 40]
[10, 20, 30, 40]

4. # Creating a List with the use of a String
 # एक स्ट्रिंग के उपयोग के साथ एक लिस्ट बनाना
List = [‘You create Your own reality’]
print(“\n List with the use of String: “)
print (List)

Output:
List with the use of String:
[‘You create Your own reality’]

5. Creating a List with the use of multiple values
कई वैल्यू के उपयोग के साथ एक लिस्ट बनाना
List = [“apple”, “banana”, “Pine”]
print(“\n List containing multiple values: “)
print (List)

Output:
List containing multiple values:
[‘apple’, ‘banana’, ‘Pine’]

6. Creating a Multi-Dimensional List (By Nesting a list inside a List)
मल्टी-डॉयमेंशनल लिस्ट बनाना (किसी सूची के अंदर सूची बनाना)
List = [[‘Apple’, ‘Banana’], [‘Onion’, ‘Potato’]]
print(“\n Multi-Dimensional List : “)
print (List)

Output:
Multi-Dimensional List :
[[‘Apple’, ‘Banana’], [‘Onion’, ‘Potato’]]

7. Creating a List having duplicate values
डुप्लिकेट वैल्यू वाले एक लिस्ट बनाना
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print(“\n List having duplicate values: “)
print (List)

Output:
List having duplicate values:
[1, 2, 4, 4, 3, 3, 3, 6, 5]

8. Creating a List with mixed type of values
मिक्स टाइप के मूल्यों के साथ एक सूची बनाना
List = [101, ‘Akhara’, 5.6, True, 1+2j]
print(“\n List with the use of Mixed Values: “)
print (List)

Output:
List with the use of Mixed Values:
[101, ‘Akhara’, 5.6, True, (1+2j)]

9. Create list with list() function
list() फंक्शन के साथ लिस्ट बनायें

The list() function creates a list in Python.
list() फंक्शन पायथन में एक सूची बनानता है ।

Syntax: list([iterable])

  • Python list() function takes a single argument.

पायथन  list() फंक्शन एक सिंगल तर्क लेता है ।

  • iterable (optional) – an object that could be a sequence (string, tuple) or collection (set, dictionary) or iterator object

iterable (वैकल्पिक)- एक वस्तु जो एक सिक्वेन्स (स्ट्रिंग, टपल) या कलेक्शन (सेट, डिक्शनरी) या पुनरावृत्त ऑब्जेक्ट हो सकती है।

Return value from list(): The list() function returns a mutable sequence list of elements.

List() से वैल्यू रिटर्न करें- list() फंक्शन एलिमेंट की एक परस्पर सीक्वेंस सूची देता है ।

  • If no parameters are passed, it creates an empty list

यदि कोई पैरामीटर पारित नहीं किया जाता है, तो यह एक इम्पटी सूची बनाता है।

  • If iterable is passed as parameter, it creates a list of elements in the iterable.

यदि iterable को पैरामीटर के रूप में पास किया जाता है, तो वह iterable में एलीमेंट की एक सूची बनाता है।

Example:
# empty list
print (list ())
# vowel string
s = ‘Be a Master’
print (list(s))
# vowel list
vowelList = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
print (list (vowelList))

Ouput:
[]
[‘B’, ‘e’, ‘ ‘, ‘a’, ‘ ‘, ‘M’, ‘a’, ‘s’, ‘t’, ‘e’, ‘r’]
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]

10. Create list with split() function
split() फंक्शन के साथ लिस्ट बनाना

In Python, you may need to break a large string down into smaller strings then we use the split function. What it does is split a string and add the data to a string array using a defined separator. By default, separator is whitespace.

पायथन में, आपको बड़े स्ट्रिंग को छोटे स्ट्रिंग में तोड़ने की आवश्यकता हो सकती है तब हम विभाजन फंक्शन का उपयोग करते हैं। यह एक स्ट्रिंग को विभाजित करता है और एक परिभाषित विभाजक का उपयोग करके डेटा को स्ट्रिंग ऐरे में जोड़ता है। डिफॉल्ट रूप से विभाजक व्हाइट स्पेस होता है।

Syntax: str.split([separator [, max-split]])

The split() method takes maximum of 2 parameters:
split() मेथड अधिकतम दो पैरामीटर लेती है:

separator (optional): The is a delimiter. The string splits at the specified separator. If the separator is not specified, any whitespace (space, newline etc.) string is a separator.

सेपरेटर (वैकल्पिक): एक सीमांकक है। स्ट्रिंग निर्दिष्ट विभाजक पर विभाजित होती है । यदि विभाजक निर्दिष्ट नहीं है, तो कोई भी व्हाइट स्पेस (स्पेस, न्यूलाइन आदि) स्ट्रिंग एक विभाजक है।

  • max-split (optional): The max-split defines the maximum number of splits. The default value of max-split is -1, meaning, no limit on the number of splits.

मैक्स-स्पिल्टि (वैकल्पिक): max-split अधिकतम संख्या विभाजन को परिभाषित करता है। max-split का डिफाल्ट वैल्यू -1 है, जिसका अर्थ है, विभाजन की संख्या पर कोई सीमा नहीं है।

Return Value from split(): The split() breaks the string at the separator and returns a list of strings.

split() के लिए रिटर्न वैल्यू split() सेपरेटर स्ट्रिंग को तोड़ता है और स्ट्रिंग की सूची लौटाता है ।

Example 1:
str1 = “Be a Creator”
# splits at space
print (str1.split ())
str2 = “banana, mango, apple”
# splits at ‘,’
print (str2.split(‘,’))
str3 = “1:2:3:4:5:6:7:8:9”
# Splitting at ‘:’
print (str3.split(‘:’))
str4 = “hello world!”
# splitting at ‘@’
print (str4.split(‘@’))

Output:
[‘Be’, ‘a’, ‘Creator’]
[‘banana’, ‘ mango’, ‘ apple’]
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
[‘hello world!’]

Example 2:
using max-split – max-split का उपयोग करना
nums = ‘1, 2, 3, 4, 5, 6, 7, 8, 9 ‘
# maxsplit: 2
print (nums.split(‘, ‘,2))
# maxsplit: 1
print (nums.split(‘, ‘, 1))
# maxsplit: 5
print (nums.split(‘, ‘, 5))
# maxsplit: 0
print (nums.split(‘, ‘,0))

Output:
[‘1’, ‘2’, ‘3, 4, 5, 6, 7, 8, 9 ‘]
[‘1’, ‘2, 3, 4, 5, 6, 7, 8, 9 ‘]
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6, 7, 8, 9 ‘]
[‘1, 2, 3, 4, 5, 6, 7, 8, 9 ‘]

Chapter wise Model Paper Link

About Me