Python Programming List method full details
How to download python?
To download the python latest version click this link.
To download the Py charm click this link.
Day 1: Python learn to start on 6/1/2021
Topic: Some Method of the list:
Method Name
# 1. To add element on the list or in other also we can use append( ) and extend( )
- .append( )
we can use .append( ) in two ways i.
code:
we use clear( ) method to remove all the element from the list
We use copy( ) method to copy list for example:
list = [1, 4, 9, 'girl', True ]
list2 = ["hari", False, 45, 77]
list.append(list2)
print(list)
Result:
[1, 4, 9, 'girl', True, ['hari', False, 45, 77]]
To add multi-element we use extend() which we will discuss below.
ii.
Simply we use append( ) method for add only one element example:
Code:
list = [1, 4, 9, 'girl', True ]
list.append(45)
print(list)
.extend( )
We use extend( ) method for adding multi-element. Example given below:
Code:
list = [1, 4, 9, 'girl', True ]
list2 = ["hari", False, 45, 77]
list.extend(list2)
print(list)
Result:
[1, 4, 9, 'girl', True, 'hari', False, 45, 77]
we don't use extend( ) for adding one element on a list if we do that error occur:
example:
Code:
list = [1, 4, 9, 'girl', True ]
list.extend(45)
print(list)
Result:
Traceback (most recent call last):
File "d:\New folder\second.py", line 2, in <module>
list.extend(45)
TypeError: 'int' object is not iterable
- insert( )
We use insert( ) to insert the element in a specified position simply use list.insert(pos, elmnt)
were, pos. = the position where you want to add the element and elmnt. = element where you can give the element want to insert.
for example:
a = [2, 4, 8, 10, 12]
a.insert(2,6)
print(a)
Result:
[2, 4, 6, 8, 10, 12]
Note:-we count from 0 1 2 3 4....
#2. To remove the element we use .clear( ), pop( ), remove( ), and .del( )
- clear( )
we use clear( ) method to remove all the element from the listexample:
code:
list = [1, 4, 9, 'girl', True ]
list.clear()
print(list)
Result:
[ ]
- pop( )
example:
code:
fruits = ['apple', 'bannana', 'charry', 'kiwi']
fruits.pop(1)
print(fruits)
Here fruits are the name of the list, inside the square bracket there is a thing on the list. we use pop(1) to remove the second element of the list, where 1 is the position of the element.
note: in python, we start counting from 0 so, in the above example 1 = the second element
Result:
['apple', 'charry', 'kiwi']
If we don't write the position in pop( ) method then it automatically removes the last element. list
for example:
fruits = ['apple', 'bannana', 'charry', 'kiwi']
fruits.pop()
print(fruits)
Result:
['apple', 'bannana', 'charry']
- remove( )
We use remove( ) method to remove the element from list but we only remove one element in remove()
code:
fruits = ['apple', 'bannana', 'charry', 'kiwi']
fruits.remove("kiwi")
print(fruits)
Result:
['apple', 'bannana', 'charry']
Day 2:
Today we are going to learn other method use in list: like copy( ), count( ), index( ), reverse( ) and sort( ).
- copy( )
We use copy( ) method to copy list for example:fruits = ['apple', 'bannana', 'charry', 'kiwi']
fruits2=fruits.copy()
print(fruits2)
Result:
['apple', 'bannana', 'charry', 'kiwi']
- count( )
We use the count() method to count the number of the element in the list for example:a = [34, 45, 93, 34, 65, 34, 93, 34]print(a.count(34))
a = [34, 45, 93, 34, 65, 34, 93, 34]
print(a.count(34))
Result:
4
- index( )
To find the position of the element we use index () method for example:
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)
Result:
2
- reverse( )
We can use reverse( ) to reverse the list for example:
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
Result:
['cherry', 'banana', 'apple']
- sort( )
We use sort( ) to arrange the element of list in order for example
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
Result:
['BMW', 'Ford', 'Volvo']
We also arrange in different ways by using a function in sort( ) for example
# A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(reverse=True, key=myFunc)
print(cars)
Result:
['Mitsubishi', 'Ford'', 'BMW', 'VW']
Comments
Post a Comment