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.
- The List is always enclosed in Square-Brackets.
- The list can contain any data type.
- It is dynamic, which means it does not have a fixed size and expands as more values are added to it.
- Lists are ordered, which means it maintains the order of elements in which they are inserted into the list.
>>> l = [1, 2, 3.02, 'Neeraj']
>>>l = [1,2,3]
>>>print( len(l) )
>>>l = l + [5]
>>>print( len(l) )
3
4
>>>print( len(l) )
>>>l = l + [5]
>>>print( len(l) )
3
4
>>>l = [1,2,3,4,5,6,7]
>>>print(l)
[1,2,3,4,5,6,7]
>>>print(l)
[1,2,3,4,5,6,7]
Python List append()
Add a single element to the end of list.
Syntax
list.append(item)
Return Value
NonePython List extend()
Add elements of another list to the list.
Syntax
list1.extend(list2)
Return Value
NonePython List insert()
Add a single object at a particular index.
Syntax
list.insert(index, object)
Return Value
NonePython List remove()
Removes the first occurence of an item from list
Syntax
list.remove(element)
Return Value
NonePython 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
NonePython List sort()
Sorts the elements of a List in ascending order.
Syntax
list.sort()
Return Value
NonePython List clear()
Removes all the elements of List.
Syntax
list.clear()
Return Value
NonePython 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
Thank You

0 Comments