About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Tuesday, September 3, 2019

Commonly used List methods in Python

Below are the basic frequently used methods of List

list1 = [1,2,3]
list1.append(
3)
print('1.Append: After appending value 3 at the end of list1 : ',list1)
Output:
1.Append: After appending value 3 at the end of list1 :  [1, 2, 3, 3]

list2 = [
10,11]
list1.append(list2)
print('\n2.Append: After appending list2 with list1 : ',list1)
Output
2.Append: After appending list2 with list1 :  [1, 2, 3, 3, [10, 11]]

list1.insert(
5,4)
print('\n3.Insert: After inserting value 4 at postion 5 : ',list1)
Output
3.Insert: After inserting value 4 at postion 5 :  [1, 2, 3, 3, [10, 11], 4]

list1.insert(
6,list2)
print('\n4.Insert: After inserting value list2 [10,11] at postion 6 : ',list1)
Output
4.Insert: After inserting value list2 [10,11] at postion 6 :  [1, 2, 3, 3, [10, 11], 4, [10, 11]]

a= list1.count([
10,11])
print('\n5.Count: Count the occurance of sublist [10,11] in list1: ',a)
Output:
5.Count: Count the occurance of sublist [10,11] in list1:  2

list1.pop()
print('\n6.Pop: Poping up of last item of the list:',list1)
Output:
6.Pop: Poping up of last item of the list: [1, 2, 3, 3, [10, 11], 4]

list1.extend(list2)
print('\n7.Extend: Append will add as element whereas Extend will add as value:',list1)
Output:
7.Extend: Append will add as element whereas Extend will add as value: [1, 2, 3, 3, [10, 11], 4, 10, 11]

print('\n8.Reverse: List before sorting :',list2)
list2.sort(
reverse=True)
print('\n8.ReverseList after sorting :',list2)
Output:
8.Reverse: List before sorting : [10, 11]
8.ReverseList after sorting : [11, 10]

i=list1.index(
3)
print('First occurance of 3 in the list', i)
Output:
First occurance of 3 in the list 2

print('To use Min or Max from list, list should not be nested list')
print('Minimum from the list2 :', min(list2), '\nMaximum from the list2  :', max(list2))
Output:
To use Min or Max from list, list should not be nested list
Minimum from the list2 : 10
Maximum from the list2  : 11


print('del and Remove, del take index, remove take value to be deleted from list')
print('Before Del, list1 is :', list1)
del list1[0]
print('Del 0 index element from the list, new list :', list1)
list1.remove([
11,10])
print('After removing [10,11] from the list1 :', list1)
Output:
del and Remove, del take index, remove take value to be deleted from list
Before Del, list1 is : [1, 2, 3, 3, [11, 10], 4, 10, 11]
Del 0 index element from the list, new list : [2, 3, 3, [11, 10], 4, 10, 11]
After removing [10,11] from the list1 : [2, 3, 3, 4, 10, 11]


Data Science with…Python J
Post Reference: Vikram Aristocratic Elfin Share

No comments:

Post a Comment