Skip to main content

List in Python पायथन में लिस्ट, List operations लिस्ट ऑपरेशन, List functions and methods लिस्ट फंक्शनस एवं मेथड्स

In Python, a list is a mutable, ordered collection of elements. Lists are one of the most commonly used data structures in Python because they can store elements of various data types and allow for flexible operations such as adding, removing, and modifying elements. Lists are defined by enclosing elements in square brackets [], with elements separated by commas.

पायथन में, लिस्ट (सूची) तत्वों का एक परिवर्तनशील, क्रमबद्ध संग्रह है। पायथन में सूचियाँ सबसे अधिक इस्तेमाल की जाने वाली डेटा संरचनाओं में से एक हैं क्योंकि वे विभिन्न डेटा प्रकारों के तत्वों को संग्रहीत कर सकती हैं और तत्वों को जोड़ने, हटाने और संशोधित करने जैसे लचीले संचालन की अनुमति देती हैं। सूचियों को वर्गाकार कोष्ठकों [] में तत्वों को संलग्न करके परिभाषित किया जाता है, जिसमें तत्वों को अल्पविराम से अलग किया जाता है।
Syntax:-

# Creating a list लिस्ट तैयार करना 

my_list = [1, 2, 3, "apple", [4, 5]]

# Lists can be nested लिस्ट को नेस्ट किया जा सकता है 

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1]) # Output: [4, 5, 6]
print(matrix[1][2]) # Output: 6

List operations लिस्ट ऑपरेशन:-

1. Concatenation जोड़ना:- We can concatenate two or more lists using the + operator.
हम + ऑपरेटर का उपयोग करके दो या अधिक सूचियों को जोड़ सकते हैं।

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]

2. Repetition दोहराव:- We can repeat a list multiple times using the * operator.
हम * ऑपरेटर का उपयोग करके किसी सूची को कई बार दोहरा सकते हैं।
my_list = [1, 2, 3]
print(my_list * 3) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

3. Indexing अनुक्रमाणिका:- We can access elements in a list by their index, starting at 0.
हम किसी सूची में तत्वों तक उनके सूचकांक (0 से शुरू करके) के आधार पर पहुंच सकते हैं।

my_list = [10, 20, 30, 40]
print(my_list[0]) # Output: 10
print(my_list[2]) # Output: 30

4. Slicing स्लाइसिंग:- We can access a range of elements using slicing.
स्लाइसिंग का उपयोग करके हम अनेक तत्वों तक पहुँच सकते हैं।

my_list = [10, 20, 30, 40, 50]
print(my_list[1:4]) # Output: [20, 30, 40]

5. Membership Testing मेम्बरशिप की जांच:- We can check if an element exists in a list using the in keyword.
हम in कीवर्ड का उपयोग करके यह जांच सकते हैं कि कोई तत्व सूची में मौजूद है या नहीं।

my_list = [10, 20, 30]
print(20 in my_list) # Output: True
print(50 in my_list) # Output: False

6. Iteration पुनरावृत्ति:- We can iterate through the elements of a list using a for loop.
हम फॉर लूप का उपयोग करके सूची के तत्वों के माध्यम से पुनरावृत्ति कर सकते हैं।

my_list = [1, 2, 3]
for elem in my_list:
   print(elem)

# Output:-
1
2
3

7. List Comprehension लिस्ट कॉम्प्रिहेंशन:- List comprehensions provide a concise way to create lists.
सूची समझ सूचियाँ बनाने का एक संक्षिप्त तरीका प्रदान करती है।

squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]

List functions and methods लिस्ट फंक्शनस एवं मेथड्स :-

1. len():- Returns the number of elements in the list.
यह फंक्शन सूची में तत्वों की संख्या लौटाता है।

my_list = [1, 2, 3, 4]
print(len(my_list)) # Output: 4

2. max():- Returns the maximum element in the list (if the elements are comparable).
यह फंक्शन सूची में अधिकतम तत्व लौटाता है (यदि तत्व तुलनीय हैं)।

my_list = [1, 5, 3, 7]
print(max(my_list)) # Output: 7

3. min():- Returns the minimum element in the list (if the elements are comparable).
यह फंक्शन सूची में न्यूनतम तत्व लौटाता है (यदि तत्व तुलनीय हैं)।

my_list = [1, 5, 3, 7]
print(min(my_list)) # Output: 1

4. sum():- Returns the sum of all numeric elements in the list.
यह फंक्शन सूची में सभी संख्यात्मक तत्वों का योग लौटाता है।

my_list = [1, 2, 3, 4]
print(sum(my_list)) # Output: 10

5. sorted():- Returns a sorted copy of the list without modifying the original list.
यह फंक्शन मूल सूची को संशोधित किए बिना सूची की क्रमबद्ध प्रतिलिपि लौटाता है।

my_list = [3, 1, 4, 2]
sorted_list = sorted(my_list)
print(sorted_list) # Output: [1, 2, 3, 4]
print(my_list) # Output: [3, 1, 4, 2]

6. list():- Creates a list from an iterable (such as a string, tuple, or set).
यह फंक्शन किसी पुनरावृत्तीय (जैसे कि स्ट्रिंग, टपल या सेट) से सूची बनाता है।

my_string = "hello"
my_list = list(my_string)
print(my_list) # Output: ['h', 'e', 'l', 'l', 'o']

7. append():- Adds an element to the end of the list.
यह फंक्शन सूची के अंत में एक तत्व जोड़ता है

my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]

8. extend():- Extends the list by appending all elements from another list or iterable.
यह फंक्शन किसी अन्य सूची या पुनरावृत्तीय से सभी तत्वों को जोड़कर सूची का विस्तार करता है

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]

9. insert():- Inserts an element at a specific position.
यह फंक्शन किसी तत्व को किसी विशिष्ट स्थान पर सम्मिलित करता है।

my_list = [1, 2, 4]
my_list.insert(2, 3) # Insert 3 at index 2
print(my_list) # Output: [1, 2, 3, 4]

10. remove():- Removes the first occurrence of a specific value.
यह फंक्शन किसी विशिष्ट मान की पहली घटना को हटाता है।

my_list = [1, 2, 3, 4, 3]
my_list.remove(3)
print(my_list) # Output: [1, 2, 4, 3]

11. pop():- Removes and returns the element at a specific position (default is the last element).
यह फंक्शन तत्व को किसी विशिष्ट स्थान पर हटाता है और लौटाता है (डिफ़ॉल्ट अंतिम तत्व है)

my_list = [1, 2, 3]
last_element = my_list.pop()
print(last_element) # Output: 3
print(my_list) # Output: [1, 2]

12. clear():- Removes all elements from the list, leaving it empty.
यह फंक्शन सूची से सभी तत्वों को हटा देता है, तथा उसे रिक्त छोड़ देता है।

my_list = [1, 2, 3]
my_list.clear()
print(my_list) # Output: []

13. index():- Returns the index of the first occurrence of a specific value. Raises a ValueError if the value is not found.
यह फंक्शन किसी विशिष्ट मान की पहली घटना का सूचकांक लौटाता है। यदि मान नहीं मिलता है तो ValueError प्रदर्शित करता है।

my_list = [1, 2, 3, 4, 3]
print(my_list.index(3)) # Output: 2

14. count():- Returns the number of times a specified value appears in the list.
यह फंक्शन सूची में निर्दिष्ट मान के प्रकट होने की संख्या लौटाता है

my_list = [1, 2, 3, 3, 4]
print(my_list.count(3)) # Output: 2

15. sort():- Sorts the list in ascending order (modifies the original list). 
यह फंक्शन सूची को आरोही क्रम में सॉर्ट करता है (मूल सूची को संशोधित करता है)। 

my_list = [3, 1, 4, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]

To sort in descending order, use reverse=True. अवरोही क्रम में सॉर्ट करने के लिए, रिवर्स=ट्रू का उपयोग करें।

my_list.sort(reverse=True)
print(my_list) # Output: [4, 3, 2, 1]

16. reverse():- Reverses the order of the elements in the list.
यह फंक्शन सूची में तत्वों का क्रम उलट देता है

my_list = [1, 2, 3, 4]
my_list.reverse()
print(my_list) # Output: [4, 3, 2, 1]

17. copy():- Creates a shallow copy of the list.
यह फंक्शन सूची की एक शाल्लो प्रतिलिपि बनाता है

my_list = [1, 2, 3]
copy_list = my_list.copy()
print(copy_list) # Output: [1, 2, 3]

Comments

Popular posts from this blog

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

Inbuilt Functions in Python पाइथन में अंतर्निहित फ़ंक्शन्स

Python is a powerful programming language that provides many inbuilt functions to make coding simple and efficient. These functions help programmers perform common tasks like finding length, converting characters, or checking memory addresses without writing extra code. पायथन एक शक्तिशाली प्रोग्रामिंग भाषा है जो कई अंतर्निहित (inbuilt) फ़ंक्शन्स प्रदान करती है, जिससे कोडिंग आसान और प्रभावी हो जाती है। ये फ़ंक्शन प्रोग्रामर को सामान्य कार्य करने में मदद करते हैं जैसे लंबाई निकालना, अक्षरों का रूपांतरण करना या मेमोरी एड्रेस जांचना, और इसके लिए अतिरिक्त कोड लिखने की आवश्यकता नहीं होती। (a)  id()  – Unique Identifier of an Object  किसी वस्तु का अद्वितीय पहचानकर्ता The  id()  function returns the unique memory address of an object. Every variable or object in Python is stored in memory, and  id()  helps us know where exactly it is stored. id()  फ़ंक्शन किसी ऑब्जेक्ट का अद्वितीय मेमोरी एड्रेस लौटाता है। पायथन में हर वेरिएबल या ऑब्जेक्ट मेमोरी में संग्रह...

Conditional statements in Python if, if-else, if-elif-else पायथन में सशर्त कथन

  In Python, conditional statements help you make decisions in the code. There are three main types of conditionals:- पायथन में, सशर्त कथन आपको कोड में निर्णय लेने में मदद करते हैं। सशर्त के तीन मुख्य प्रकार हैं:-  1.) Conditional `if` statement:- An `if` statement executes the code block only if the condition evaluates to `True`. 1.) सशर्त `if` कथन:- एक `if` स्टेटमेंट कोड ब्लॉक को केवल तभी निष्पादित करता है जब स्थिति सही पर मूल्यांकन करती है। Syntax:- if condition:    Statement(s) Example:- x = 10 if x > 5:     print("x is greater than 5") 2.) Alternative `if-else` statement:  An `if-else` statement checks a condition and executes a block of code('if' block), if the condition is true otherwise provides an alternative block of code ('else' block), if the condition is false. 2.) वैकल्पिक `if- else` कथन:- एक `if-else` स्टेटमेंट एक शर्त की जाँच करता है और कोड के एक ब्लॉक ('if' ब्लॉक) को निष्पादित करता है, यदि स्थिति सही है अन्यथा कोड का एक वैकल्पिक ब्ल...