Loading...
Loading...
00:00:00

Python List and It's Functions

A list in Python is an ordered collection of items, which can be of any data type, including numbers, strings, and other objects. Lists are defined using square brackets [] and items are separated by commas:

fruits = ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4, 5]

Lists in Python are mutable, which means you can add, remove, or modify items after the list has been created. For example:

# Adding an item to the end of a list
fruits.append('orange')
print(fruits)  # ['apple', 'banana', 'cherry', 'orange']

# Removing an item from a list
fruits.remove('banana')
print(fruits)  # ['apple', 'cherry', 'orange']

# Modifying an item in a list
numbers[2] = 10
print(numbers)  # [1, 2, 10, 4, 5]

You can access items in a list by using an index, which starts at 0 for the first item and increases by 1 for each subsequent item:

print(fruits[0])  # 'apple'
print(fruits[1])  # 'cherry'

List() Function

list() function is used to create a list or typecast other types of iterable object as list. list function takes as argument iterable for create a new list.

a = list((10,20,30,59,29))
print(a)      # [10, 20, 30, 59, 29]    
a = (39,29,12,8,99)
print(list(a))    # [39, 29, 12, 8, 99]
a = list(("hello World"))
print(a)      # ['h', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

Accessing List Items Using Their Index

List item can be access using their index . index startswith 0 and endswith size-1 .

a = [10,20,30,59,29]
print(a[0])      # 10
print(a[1])      # 20
print(a[2])      # 30
print(a[3])      # 59
print(a[4])      # 29

Modifying List

we can modify the list and change .

a = [10,20,30,59,29]
a[2] = 200
a[4] = 77
print(a[0])      # 10
print(a[1])      # 20
print(a[2])      # 200
print(a[3])      # 59
print(a[4])      # 77

List methods(functions) in Python

Method Uses
count() Returns the number of elements in a list as specified
clear() Removes all the elements from the list
copy() Returns a copy of list
extend() Add the elements in a list at the end
index() Returns the index of the item at given position
insert() Add an item at the specified position
append() Add an element at the end of the list
pop() Remove an last item of list or at the specified position
remove() Removes the specified item
reverse() Reverses the order of list
sort() Sorts the list

List Pop() Methods

remove the last item or from specified position.

a = [10,29,93,-82,2,87]
print(a.pop())     #    87
print(a.pop(4))     #    2

List Append() Methods

Add item at the ends of the list

a = [10,29,93,-82,2,87]
a.append(55)    
print(a)     #    [10, 29, 93, -82, 2, 87, 55]

List Count() Methods

count how many item present in a list as specified item.

a = [10,29,93,-82,2,87]
print(a.count(20))    # 0
print(a.count(29))    # 1

List Clear() Method

clear all item from the list return as empty

a = [10,29,93,-82,2,87]
a.clear()
print(a)    # []

List Remove() Method

remove an item from the list

a = [10,29,93,-82,2,87]
a.remove(-82)
print(a)    # [10, 29, 93, 2, 87]

List Reverse() Method

reverse the list

a = [10,29,93,-82,2,87]
a.reverse()
print(a)    # [87, 2, -82, 93, 29, 10]