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

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...