Important Notice:

Course Content
List Manipulation in Python (Chapter-7) M3-R5
About Lesson

4. extend():-

          इस method का use पहले से बनी लिस्ट के last में किसी दूसरी लिस्ट को जोड़ने के लिए किया जाता है।

Or

    इस method का use लिस्ट के last में एक ही समय में कई element को जोड़ने के लिए किया जाता है।

a=[10,20,30]

b=[50,60,70]

a.extend(b)

print(a)                       # [10, 20, 30, 50, 60, 70]

Note:- यदि iterable object एक string है तो प्रत्येक character individually add होता है।

a=[10,20,30]

a.extend(‘word’)

print(a)                       # [10, 20, 30, ‘w’, ‘o’, ‘r’, ‘d’]

Note:- append() & extend() method केवल last में element को add करता है।

5. insert():-

         यह method element को किसी भी index number पर जोड़ने के लिए किया जाता है। जो भी remaining element होता है वह right side में shift हो जाता है।

Syntax:- list.insert(position, element)

a=[10,20,30]

a.insert(1,50)

print(a)                                    # [10, 50, 20, 30]

6. pop():-

         इस method का use list से दिये गये position number से item को remove करने के लिए किया जाता है। यदि index number नही दिया जायेगा तो list से last item को remove किया जाता है।

Syntax:-           pop(index number)

1. No Index specified

a=[10,20,30]

a.pop()

print(a)                            # [10, 20]

2. Index specified

a=[10,20,30]

a.pop(1)

print(a)                            # [10, 30]

 

List Functions & Methods 2

4. extend():-

This method is used to add another list to the end of the already created list.

Or

This method is used to add multiple elements to the last of the list at the same time.

a=[10,20,30]

b=[50,60,70]

a.extend(b)

print(a)                     # [10, 20, 30, 50, 60, 70]

Note:- If the iterable object is a string then each character is added individually.

a=[10,20,30]

a.extend(‘word’)

print(a)                                          # [10, 20, 30, ‘w’, ‘o’, ‘r’, ‘d’]

Note:- append() and extend() method appends only the last element.

5. insert():-

    This method is used to add the element at any index number. Whatever remaining element is shifted to the right side.

Syntax:- list.insert(position, element)

a=[10,20,30]

a.insert(1,50)

print(a)                                            # [10, 50, 20, 30]

6. pop():-

   This method is used to remove the item from the list at a given position number. If index number is not given then the last item is removed from the list.

Syntax:-     pop(index number)

1. No Index specified

a=[10,20,30]

a.pop()

print(a)                        # [10, 20]

2. Index specified

a=[10,20,30]

a.pop(1)

print(a)                            # [10, 30]

error: Content is protected !!