Python Lists

Python Lists

Python में list का उपयोग विभिन्न प्रकार के डेटा को sequence में स्टोर करने के लिए किया जाता है। python में list परिवर्तनशील होते हैं, इसका मतलब है कि हम इसके elements को बनाने के बाद modify भी कर सकते हैं। हालाँकि, python में छह Data types होते हैं, लेकिन सबसे सामान्य और विश्वसनीय type list होता है।

List को विभिन्न प्रकार के value या items के रूप में परिभाषित किया जा सकता है। List में items को comma (,) से अलग किया जाता हैं और square brackets [] के साथ enclose किया जाता हैं।

जैसे

L1 = [“John”, 205, “England”]

L2 = [1, 2, 3, 4, 5, 6]

 

Characteristics of Lists

List में निम्नलिखित विशेषताए होती हैं-


  1. List क्रम (ordered) में होती हैं|
  2. List के element को index के द्वारा एक्सेस कर सकते हैं|
  3. List परिवर्तनशील होते हैं|
  4. List विभिन्न element की संख्या को store कर सकती है।

List indexing and splitting

Indexing उसी तरह Process करता है जिस तरह यह स्ट्रिंग्स के साथ होता है। slice operator [] का उपयोग करके list के element तक पहुँचा जा सकता है।

index 0 से शुरू होता है और लंबाई 1 तक जाता है, list का पहला element, 0 index में store किया जाता है,और list का दूसरा element 1 index पर store किया जाता है, और इसी तरह यह आगे के elements को स्टोर करता जाता हैं|

List = [0, 1, 2, 3, 4, 5]

हम निम्नलिखित syntax का उपयोग करके list की sub list प्राप्त कर सकते हैं।

list_varible(start:stop:step)

Start – यह लिस्ट की starting index स्थिति को दर्शाता है।


Stop – यह लिस्ट की last index स्थिति को दर्शाता है।

Step – इसका उपयोग nth एलिमेंट को start:stop के अंदर skip करने के लिए किया जाता है|

अन्य भाषाओं के विपरीत, python Negative index का उपयोग करने के लिए भी सुविधा प्रदान करता है। Negative index की गणना दाईं ओर से की जाती है।

Updating List values

पाइथन में lists सबसे बहुमुखी (more versatile) data structures हैं, क्योंकि यह परिवर्तनीय हैं, और उसकी Value को slice और assignment operator का उपयोग करके अपडेट किया जा सकता है।

python append () और insert () methods भी प्रदान करता है, जिनका उपयोग list में value जोड़ने के लिए किया जा सकता है।

List के अंदर value को update करने के लिए निम्नलिखित उदाहरण –

list = [‘physics’, ‘chemistry’, 2000, 2020]

print [“Value available at index 2 : “]

print list[2]

list[2] = 2021;

print [“New value available at index 2 : “]

print list[2]

जब उपरोक्त कोड execute किया जाता है, तो यह result देता हैं

Value available at index 2 :

2000

New value available at index 2 :

2021

Delete list element

आप list element को हटाने के लिए Del कीवर्ड का उपयोग कर सकते है। यदि हम नहीं जानते कि list से कौन सा element हटाना है, तो python हमें remove () method भी प्रदान करता है।

list1 = [‘physics’, ‘chemistry’, 1997, 2000]

print [list1]

del list1[2];

print [“After deleting value at index 2 : “]

print [list1]

जब उपरोक्त कोड execute किया जाता है, तो यह result देता हैं

[‘physics’, ‘chemistry’, 1997, 2000]

After deleting value at index 2 :

[‘physics’, ‘chemistry’, 2000]

Python List Operations

concatenation (+) और repetition (*) ऑपरेटर उसी तरह काम करते हैं जैसे यह स्ट्रिंग्स के साथ काम करते हैं|

Python Expression

Results

Description

len([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation
[‘Hi!’] * 4 [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’] Repetition
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]: print x, 1 2 3 Iteration

Iterating a List

किसी list को for-in loop का उपयोग करके iterate किया जा सकता है। चार स्ट्रिंग वाली साधारण list, जिसे निम्नानुसार iterate किया जा सकता है।

Adding elements to the list

python append () Function प्रदान करता है जिसका उपयोग list में element जोड़ने के लिए किया जाता है। हालाँकि, append () Function केवल list के अंत में value जोड़ सकता है।

How to append element in the list

python list में elements को append या जोड़ने के लिए built-in method प्रदान करता है। हम एक list को दूसरी list में भी जोड़ सकते हैं। ये तरीके नीचे दिए गए हैं।

append(elmt) – यह list के अंत में value जोड़ता है।

insert(index, elmt) – यह specified index position पर वैल्यू इन्सर्ट करता है।

extends(iterable) – यह iterable object को जोड़कर list को extend करता है।

आइए इन विधियों को निम्नलिखित उदाहरण से समझते हैं।

  1. append(elmt)

इस Function का उपयोग list के अंत में element जोड़ने के लिए किया जाता है।

Example

names = [“Ashish”, “Shiva”, “Anchal”, “Anurag”]

print(‘Current names List is:’, names)

new_name = input(“Please enter a name:\n”)

names.append(new_name)  # Using the append() function

print(‘Updated name List is:’, names)

output

Current names List is: [‘Ashish’, ‘Shiva’, ‘Anchal’, ‘Anurag’]

Please enter a name:

Nutan

Updated name List is: [‘Ashish’, ‘Shiva’, ‘Anchal’, ‘Anurag’, ‘Nutan’]

  1. insert(index, elmt)

insert () Function दिए गए इंडेक्स पोजीशन पर एलिमेंट्स को जोड़ता है। यह तब फायदेमंद होता है जब हम किसी specific position पर element insert करना चाहते हैं।

Example-

list1 = [10, 20, 30, 40, 50]

print(‘Current Numbers List: ‘, list1)

el = list1.insert(3, 77)

print(“The new list is: “,list1)

n = int(input(“enter a number to add to list:\n”))

index = int(input(‘enter the index to add the number:\n’))

list1.insert(index, n)

print(‘Updated Numbers List:’, list1)

output

Current Numbers List:  [10, 20, 30, 40, 50]

The new list is:  [10, 20, 30, 77, 40, 50]

enter a number to add to list:

 45

enter the index to add the number:

1

Updated Numbers List: [10, 45, 20, 30, 77, 40, 50]

  1. extend(iterable)

extend () Function का उपयोग list में iterable elements को जोड़ने के लिए किया जाता है। यह iterable object को argument के रूप में स्वीकार करता है।

Example-

list1 = [10,20,30]

list1.extend([“52.10”, “43.12” ])  # extending list elements

print(list1)

list1.extend((40, 30))  # extending tuple elements

print(list1)

list1.extend(“Apple”)  # extending string elements

print(list1)

output

[10, 20, 30, ‘52.10’, ‘43.12’]

[10, 20, 30, ‘52.10’, ‘43.12’, 40, 30]

[10, 20, 30, ‘52.10’, ‘43.12’, 40, 30, ‘A’, ‘p’, ‘p’, ‘l’, ‘e’]

Python List Built-in functions

SN

Function Description

Example

1 cmp(list1, list2) यह दोनों lists के element की तुलना करता है। इस method का उपयोग python 3 और उपरोक्त version में नहीं किया गया है।
2 len(list) इसका उपयोग list की लंबाई की गणना करने के लिए किया जाता है। L1 = [1,2,3,4,5,6,7,8]

print(len(L1))

8

3 max(list) यह list का maximum element देता है। L1 = [12,34,26,48,72]

print(max(L1))

72

4 min(list) यह list का minimum element देता है। L1 = [12,34,26,48,72]

print(min(L1))

12

5 list(seq) यह किसी भी क्रम को list में बदल देता है। str = “Johnson”

s = list(str)

print(type(s))

<class list>


error: Content is protected !!