List Methods in Python | List Methods Cheat Sheet

List Methods

Python provides list methods that allow us to work with lists.

Let’s learn few among them

  • append()
  • extend()
  • insert()
  • pop()
  • clear()
  • remove()
  • sort()
  • index()

Append

list.append(value) Adds an element to the end of the list.

Code

list_a = []
for x in range(1,4):
    list_a.append(x)
print(list_a)

Output

[1, 2, 3]

Extend

list_a.extend(list_b) Adds all the elements of a sequence to the end of the list.

Code

list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_a.extend(list_b)
print(list_a)

Output

[1, 2, 3, 4, 5, 6]

Insert

list.insert(index,value) Element is inserted to the list at specified index.

Code

list_a = [1, 2, 3]
list_a.insert(1,4)
print(list_a)

Output

[1, 4, 2, 3]

Pop

list.pop() Removes last element.

Code

list_a = [1, 2, 3]
list_a.pop()
print(list_a)

Output

[1, 2]

Remove

list.remove(value) Removes the first matching element from the list.

Code

list_a = [1, 3, 2, 3]
list_a.remove(3)
print(list_a)

Output

[1, 2, 3]

Clear

list.clear() Removes all the items from the list.

Code

list_a = [1, 2, 3]
list_a.clear()
print(list_a)

Output

[]

Index

list.index(value) Returns the index at the first occurrence of the specified value.

Code

list_a = [1, 3, 2, 3]
index =list_a.index(3)
print(index)

Output

1

Count

list.count(value) Returns the number of elements with the specified value.

Code

list_a = [1, 2, 3]
count = list_a.count(2)
print(count)

Output

1

Sort

list.sort() Sorts the list.

Code

list_a = [1, 3, 2]
list_a.sort()
print(list_a)

Output

[1, 2, 3]

Sort & Sorted

sort() Modifies the list

Code

list_a = [1, 3, 2]
list_a.sort()
print(list_a)

Output

[1, 2, 3]

sorted() Creates a new sorted list

Code

list_a = [1, 3, 2]
sorted(list_a)
print(list_a)

Output

[1, 3, 2]

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published.