Python Blogs
List in Python
- A list is a collection which is ordered and changeable.
- In python list are written with square brackets [ ].
- List is a Mutable object.
- List supports goes +ve and -ve index.
x=[10,20,30,40,50]
print(x)
Output:
[10, 20, 30, 40, 50]
Eg1:-
x=[10,20,30,40,50,60,70]
print(x)
print([0])
print([3])
print([-2])print([5])
Output:
[10, 20, 30, 40, 50, 60, 70]
[0]
[3]
[-2]
[5]
Eg2:-
x=[10,10.11,True,3+4j]
print(x)
print(x[2])
print(x[-1])
Output:
[10, 10.11, True, (3+4j)]
True
(3+4j)
LIST METHODS
Python has a set of built-in methods that you can use on lists.
| Method | Description |
| append() | Adds an element at the end of the list |
| clear() | Removes all the elements from the list |
| copy() | Returns a copy of the list |
| count() | Returns the number of elements with the specified value |
| extend() | Add the elements of a list , to the end of the current list |
| index() | Returns the index of the first element with the specified value |
| insert() | Adds an element at the specified position |
| pop() | Removes the element at the specified position |
| remove() | Removes the item with the specified value |
| reverse() | Reverses the order of the list |
| sort() | Sorts the list |
Post a Comment
0 Comments