List In Python

In this article let’s see what is a list and what are the list methods and how to perform the list methods.

What is a List?

List is a data type that contains a collection of different data items and it is enclosed by [ ] and separated by commas (,). List is mutable which means we can modify the list after list creation.

What are the list methods?

  • Append()
  • count()
  • copy()
  • clear()
  • extend()
  • insert()
  • index()
  • sum()
  • pop()
  • reverse()
  • remove()
  • sort()

list methods


append()

append() function is used to append only one element in the existing list. Append element store at the end of the list.

Syntax

list.append(element)

example

l=["hello",24,200,29.90]
l.append("python")
print(l)

List In Python

count()

count() is used to count how many times a given element occurs in the list.

Syntax

list . count(element)

Example

l=["hello",24,200,29.90]
print(l.count("hello"))

List In Python

copy()

copy() function is used to copy the elements of the list in the existing list.

Syntax

list.copy()

Example

l=["hello",24,200,29.90]
len=l.copy()
print(len)

List In Python

clear()

clear() is used to clear all the elements of the list.

Syntax

list.clear()

Example

l=["hello",24,200,29.90]
l.clear()
print(l)

List In Python

Extend()

extend() is used to extend the list and also we can add element to the list.

Syntax

list.extend(element or list)

Example

l=["hello",24,200,29.90]
m=[3,5,6,7]
l.extend(m)
print(l)

List In Python

We can add element using extend method to the list but we can display only string in a separate letter not to display words

Example

m=[3,5,6,7]
m.extend("hello,reg no")
print(m)

List In Python

index

using index () method to display the position of the element in the given list.

Syntax

list.index(element,start,end) # start and end index is optional #

example

m=[3,5,6,7,'h','i','i']
print(m.index("h"))

List In Python

example

m=[3,5,6,7,'h','i','i','o',44,55]
print(m.index('o',1,8))

List In Python

insert()

using insert() method to insert element to a given index value from the list.

Syntax

list.insert(index,element)

Example

m=[3,5,6,7,'h','i','i','o',44,55]
m.insert(1,'p')
print(m)

List In Python

sum()

sum() is used to calculate given element in the list.

Syntax

sum(list)

example

m=[5,5,2,3]
tot=sum(m)
print(tot)

List In Python

pop()

pop() is used to remove or return element in the list and also using index we can remove or return element from the list and without giving index default remove or return last element in the list.

Syntax

list.pop(index) # index is optional #

example

m=[3,5,6,7,'h','i','i','o',44,55]
m.pop()
print(m)

List In Python

example

m=[3,5,6,7,'h','i','i','o',44,55]
print(m.pop())

output

List In Python

another example of using index to remove the element in the list.

Example

m=[3,5,6,7,'h','i','i','o',44,55]
m.pop(6)
print(m)

List In Python

remove()

remove() is used to remove elements from the list.

Syntax

list.remove(element)

example

m=[3,5,6,7,'hack','i','ice','old',44,55]
m.remove('hack')
print(m)

List In Python

reverse()

we can display the list in reverse order using reverse() method

syntax

list.reverse()

example

m=[3,5,6,7,'hack','i','ice','old',44,55]
m.reverse()
print(m)

List In Python

sort()

sort() is used to sort the list in both ascending and descending order.

Syntax

list.sort()

or

list.sort( reverse=true/false , key=myfun) # reverse and key optional #

example

In this example sort() method is uded to order the list.

m=[1,2,7,3,4,6,5]
m.sort()
print(m)

List In Python

example

In this example sort the words in alphabetical order using sort()

m=["hi","every","one"]
m.sort()
print(m)

List In Python

example

In this example sort the list in reverse and alphabetical order using sort() method.

m=["hi","every","one"]
m.sort(reverse=True)
print(m)

List In Python

example

In this example using a function to pass the key parameter to sort the given list.

def mysort(el):
return el[2]
l=[(1,1,1),(3,2,2),(1,1,1),(2,1,1)]
l.sort(key=mysort)
print(l)

List In Python

I hope this article is helpful to you..


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles