Skip to main content

8.1 Python List Methods

PYTHON LISTS METHODS:
    A list is a collection which is ordered and changeable/mutable. Lists are written with square brackets. Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. It can have any number of items and they may be of different types (integer, float, string etc.). 

If you have not visited "Python Lists",check out the link below!!
Various methods can be used within Lists.Those methods includes:
append()
    The append() method is used to join the elements to the end of the existing list.
syntax:
listname.append(elements)
Program
mylist=[1,2,4,5]
mylist.append("TamilHacks")
print(mylist)
Output
[1, 2, 4, 5, 'TamilHacks']
extend()
    The extend() method is used to join the elements to the end of the existing list as separate elements.
syntax:
listname.extend(elements)
Program
mylist=[1,2,4,5]
mylist.extend("TamilHacks")
print(mylist)
Output
[1, 2, 4, 5, 'T','a','m','i','l','H','a','c','k','s']
insert()
    The insert() method is used to join/append the elements in the specified index of the list.
syntax:
listname.insert(element,indexno)
Program
mylist=[1,2,4,5]
mylist.insert(2,"TamilHacks")
print(mylist)
Output
[1, 2, 'TamilHacks',4, 5]
remove()
    The remove() method is used to remove the specified element from the list
syntax:
listname.remove(elementname)
Program
mylist=[1,2,4,5]
mylist.remove(2)
print(mylist)
Output
[1, 4, 5]
pop()
    The pop() method is used to remove the specified element from the list.The pop() method takes the index no as argument.pop() without any arguments removes the last element as default.
syntax:
listname.pop(indexid)
Program
mylist=[1,2,4,5]
mylist.pop(2)
print(mylist)
mylist.pop()
print(mylist)
Output
[1, 2, 5]
[1,2]
clear()
    The clear() method is used to empty the list.This method takes no argument.It returns empty list as output.
syntax:
listname.clear()
Program
mylist=[1,2,4,5]
mylist.clear()
print(mylist)
Output
[]
index()
    The index() method is used to return the index of the specified element.
syntax:
listname.index(element)
Program
mylist=['T','a','m','i','l','h','a','c','k','s']
print(mylist.index('m'))
Output
2
count()
    The count() method is used to return the number of times a specified element occurs in the list.
syntax:
listname.count(element)
Program
mylist=['T','a','m','i','l','h','a','c','k','s']
print(mylist.count('a'))
Output
2
sort()
    The sort() method is used to sort the elements in the list in ascending order.you can use reverse=true to sort the elements in descending order
syntax:
listname.sort()
listname.sort(reverse=True)
Program
mylist=['T','a','m','i','l','h','a','c','k','s']
mylist.sort()
print(mylist)
mylist.sort(reverse=True)
print(mylist)
Output
['a', 'a', 'c', 'h', 'i', 'k', 'l', 'm', 's', 'T']
['T', 's', 'm', 'l', 'k', 'i', 'h', 'c', 'a', 'a']
copy()
    The copy() method is used to copy the elements of list1 to list2.
syntax:
list2=list1.copy()
Program
mylist=['T','a','m','i','l','h','a','c','k','s']
xlist=mylist.copy()
print(xlist)
Output
['T','a','m','i','l','h','a','c','k','s']
reverse()
    The reverse() method is used to reverse the elements in list.
syntax:
list1.reverse()
Program
mylist=['T','a','m','i','l','h','a','c','k','s']
mylist.reverse()
print(mylist)
Output
['s','k','c','a','h','l','i','m','a','T']
sum()
        The sum() method is used return the sum of all the elements in list.
syntax:
sum(listname)
Program
mylist=[1,2,4,5]
print(sum(mylist))
Output
12
max()
     The max() method is used to return the largest(maximum) element in the list.
syntax:
max(listname)
Program
mylist=[1,2,4,5]
print(max(mylist))
Output
5
min()
The min() method is used to return the smallest(minimum) element in the list.
syntax:
min(listname)
Program
mylist=[1,2,4,5]
print(min(mylist))
Output
1
    Need to know about python,Check out the video below!!

Happy Learning
    

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl