Python_Tutorial #Class-06 | List | Learn Python



Lists are the most commonly used data structure in Python. Lists are treated as objects in Python so they have methods associated with them.
Before starting with methods associated with lists, Let us first get to a few things about lists.
  1.  The List is always enclosed in Square-Brackets.
  2. The list can contain any data type.
  3. >>> l = [1, 2, 3.02, 'Neeraj']
  4. It is dynamic, which means it does not have a fixed size and expands as more values are added to it.
  5. >>>l = [1,2,3]
    >>>print( len(l) )
    >>>l = l + [5]
    >>>print( len(l) )
    3
    4
  6. Lists are ordered, which means it maintains the order of elements in which they are inserted into the list.
  7. >>>l = [1,2,3,4,5,6,7]
    >>>print(l)
    [1,2,3,4,5,6,7]
Lists are the most commonly used data structure in Python. Lists are treated as objects in Python so they have methods associated with them some of which are mentioned below:

Python List append()

Add a single element to the end of list.

 Syntax

list.append(item)

 Return Value

None

Python List extend()

Add elements of another list to the list.

 Syntax

list1.extend(list2)

 Return Value

None

Python List insert()

Add a single object at a particular index.

 Syntax

list.insert(index, object)

 Return Value

None

Python List remove()

Removes the first occurence of an item from list

 Syntax

list.remove(element)

 Return Value

None

Python List index()

Returns the index of first occurence of the element in List.

 Syntax

list.index(element)

 Return Value

Index of element in List.

Python List count()

Returns occurence of a element in a list.

 Syntax

list.count(element)

 Return Value

Number of occurence of an element.

Python List reverse()

Reverses the order of a List

 Syntax

list.reverse()

 Return Value

None

Python List sort()

Sorts the elements of a List in ascending order.

 Syntax

list.sort()

 Return Value

None

Python List clear()

Removes all the elements of List.

 Syntax

list.clear()

 Return Value

None

Python List pop()

Returns and remove last element from the List.

 Syntax

list.pop()

 Return Value

Last item in the List.

With this, I wrap up today's class, hope you learned something new today. You can practice what you have just learned in the Python IDE below.

Thank You

Drop your queries in the comment box or mail me at [email protected]

Post a Comment

0 Comments