List Object in Python

List Object in Python

In Python, we can use ‘+’ and ‘*’ operators for list objects.

पायथन में, हम लिस्ट ऑब्जेक्ट के लिए ‘+’ और ‘*’ का उपयोग कर सकते हैं। ‘

1.Concatenation operator (+): We can use ‘+’ operator to concatenation 2 lists into a single

list. हम ‘+’ ऑपरेटर को 2 लिस्ट्स का संयोजन करके एक लिस्ट में उपयोग कर सकते हैं।

Example:

x = [10, 20, 30]
y = [40, 50, 60]
z = x + y
print (z)

Output:
[10, 20, 30, 40, 50, 60]

Note: To use ‘+’ operator compulsory both arguments should be list type objects, otherwise we will get TypeError message.

नोट: ‘+’ ऑपरेटर को अनिवार्य रूप से उपयोग करने के लिए दोनों तर्कों को लिस्ट टाईप का ऑब्जेक्ट होना चाहिए अन्यथा हमें टाइपएरर मैसेज मिलेगा।

Example:

>>> x = [10, 20]
>>> y = x + 20
Traceback (most recent call last):
  File “<pyshell#1>”, line 1, in <module>
    y = x + 20
TypeError: can only concatenate list (not “int”) to list
>>> y = x + [30]
>>> y
[10, 20, 30]

2.Repetition operator (*): We can use repetition operator *’ to repeat elements of list specified number of times.

हम पुनरावृत्ति ऑपरेटर ‘* का उपयोग लिस्ट की निर्दिष्ट संख्या में एलीमेंट को दोहराने के लिए।

Example:

x = [10, 20, 30]
z = x * 3
print (z)

Output:
[10, 20, 30, 10, 20, 30, 10, 20, 30]

Comparing list objects
लिस्ट्स ऑब्जेक्ट्स की तुलना करना
We can use comparison operators for list objects.
हम लिस्ट ऑब्जेक्ट के लिए कम्पेरिजन ऑपरेटरों का उपयोग कर सकते हैं।

Example 1:

x = [10, 20, 30]
y = [10, 20, 30]
z = [20, 30, 10]
print (x==y)
print (x==z)
print (x!=z)

Output:
True
False
True

Note: Whenever we are using comparison operators (== ,!=) for list objects then the following should be considered.

नोट: जब भी हम लिस्ट ऑब्जेक्ट के लिए कम्पेरिजन ऑपरेटरों (==,!=) का उपयोग कर रहे हैं, तो निम्नलिखित पर विचार किया जाना चाहिए।

  1. The number of elements – एलीमेंट की संख्या

  2. The order of elements – एलीमेंट का क्रम

  3. The content of elements (case sensitive) – एलीमेंट का कंटेंट (केस सेंसिटिव)

Example:

x = [‘a’, ‘b’, ‘c’]
y = [‘a’, ‘b’, ‘c’]
z = [‘A’, ‘B’, ‘c’]
print (x==y)
print (x==z)
print (x!=z)

Output:
True
False
True

Note: whenever we are using relational operators (<,<=,>,>=) between list objects, only first element comparison will be performed.

नोट: जब भी हम लिस्ट ऑब्जेक्ट्स के बीच रिलेशनल ऑपरेटर्स (<<=>>=) का उपयोग कर रहे हैं, केवल पहले एलीमेंट की तुलना की जाएगी।

Example:

x = [‘a’, ‘b’, ‘c’]
y = [‘a’, ‘b’, ‘c’]
z = [‘A’, ‘B’, ‘C’]
print (x>y)
print (x>=y)
print (x<z)
print (z<x)

Output:
False
True
False
True

Membership Operator for list objects
लिस्ट ऑब्जेक्ट्स के लिए मेम्बरशिप ऑपरेटर

In Python, we can check whether element is a member of the list or not by using membership operators. Membership operators are ‘in’ and ‘not in’.

पायथन में, हम देख सकते हैं कि मेम्बर ऑपरेटरों का उपयोग करके एलीमेंट लिस्ट का मेम्बर है या नहीं। मेंबरशिप ऑपरेटर्स ‘in’ और ‘not in’ में नहीं है ।

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below

Example

x = [‘a’, ‘b’, ‘c’]
print(‘a’ in x)
print(‘d’ in x)
print(‘b’ not in x)
print(‘e’ not in x)

Output:
True
False
False
True

clear() function: We can use clear() function to remove all elements of list.

लिस्ट के सारे एलीमेंट को रिमूव करने के लिए हम clear() फंक्शन का उपयोग कर सकते हैं।

Example:

x = [‘a’, ‘b’, ‘c’]
print (x)
x.clear()
print (x)

Output:
[‘a’, ‘b’, ‘c’]
[]

List objects are mutable i.e., we can change the content.
लिस्ट ऑब्जेक्ट्स परिवर्तनशील हैं यानी हम कंटेंट को बदल सकते हैं।

List objects are changeable object that means mutable objects, which can be modified after it is created.

लिस्ट ऑब्जेक्ट्स परिवर्तनशील ऑब्जेक्ट्स हैं जिसका अर्थ है उत्परिवर्तित ऑब्जेक्ट्स, जो बनने के बाद मॉडिफॉई की जा सकती हैं।

Example:

a = [1,2,3]
print (“Before changing list :”, a)
print (“Address of a is : “, id (a))
a [1] = 100
print (“After changing list : “, a)
print (“Address of a is : “, id (a))

Output:
Before changing list : [1, 2, 3]
Address of a is :  59510536
After changing list :  [1, 100, 3]
Address of a is :  59510536

Nested Lists नेस्टेड लिस्ट

Sometimes we can take one list inside another list. Such types of lists are called nested lists.

कभी-कभी हम एक लिस्ट को दूसरी लिस्ट के अंदर ले सकते हैं। इस प्रकार की लिस्ट को नेस्टेड लिस्ट्स कहा जाता है।

Example:

a = [10,20,[30,40]]
print (a)
print (a[0])
print (a[1])
print(a[2])
print (a [2] [0])
print(a[2] [1])

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

Note: We can access nested list elements by using index just like accessing multi dimensional array elements.

नोट: हम नेस्टेड लिस्ट एलीमेंट को मल्टी डायमेंशनल ऐरे एलीमेंटों की तरह इंडेक्स का उपयोग करके एक्सेस कर सकते हैं।

In Python, we can represent matrix by using nested lists

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

Example:

a = [[1, 2, 3] , [4, 5, 6] , [7,8,9]] 
print (a) 
print("printing elements by row wise") 
for i in a:
    print (i) 
print("printing elements by matrix style") 
for i in range (len (a)): 
    for j in range (len (a[i])):
        print (a[i] [j], end=" ") 
    print ()

Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
printing elements by row wise
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
printing elements by matrix style
1 2 3
4 5 6
7 8 9

Comprehension – कांप्रिहेंसन

Comprehensions in Python provide us with a short and concise way to construct new sequences (list, dictionary, set and generator) using sequences which have been already defined.

पायथन में, कांप्रिहेंसन हमें पहले से परिभाषित किए गए दृश्यों का उपयोग करके नए सिक्वेन्स (लिस्ट, डिक्शनरी, सेट और जनरेटर) के निर्माण के लिए एक संक्षिप्त तरीका प्रदान करता है।

List comprehension – लिस्ट कांप्रिहेंसन

List Comprehensions provide an elegant way to create new lists. The following is the basic structure of a list comprehension:

लिस्ट कांप्रिहेंसन नई लिस्ट बनाने के लिए एक अच्छा तरीका प्रदान करता है। निम्नलिखित लिस्ट कांप्रिहेंसन की मूल संरचना है

output_list = [output_expression for var in input_list if (var satisfies this condition)]

output_list = [input_list में var के लिए output_expression अगर (var इस स्थिति को संतुष्ट करता है।)

Note: The list comprehension may or may not contain an if condition. List comprehensions can contain multiple for (nested list comprehensions).

नोट: लिस्ट कांप्रिहेंसन में कोई शर्त हो सकती है या नहीं भी हो सकती है। लिस्ट कांप्रिहेंसन (नेस्टेड लिस्ट कांप्रिहेंसन) के लिए कई हो सकते हैं।

Example: Suppose we want to create an output list which contains squares of all the numbers from 1 to 10.

उदाहरण: मान लीजिए कि हम एक आउटपुट लिस्ट बनाना चाहते हैं जिसमें 1 से 10 तक की सभी संख्याएँ हों।

list = [] 
for a in range (1, 11):
    list.append (a ** 2) 
print ("Output : ", list) 
print (type (list))

Output :
Output :  [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
<class ‘list’>

Chapter wise Model Paper Link

About Me