0% found this document useful (0 votes)
31 views

Function of List

This document describes various functions that can be used on lists in Python including sort(), type(), append(), extend(), index(), max(), min(), len(), clear(), insert(), count(), pop(), remove(), reverse(), and copy().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Function of List

This document describes various functions that can be used on lists in Python including sort(), type(), append(), extend(), index(), max(), min(), len(), clear(), insert(), count(), pop(), remove(), reverse(), and copy().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FUNCTION OF LIST

 sort(): The sort() method is a built-in Python method that, by default, sorts
the list in ascending order. However, you’ll modify the order from
ascending to descending by specifying the sorting criteria.

Eg.

List=[111,112,114,121,122,124,118,119]

List.sort()

OUTPUT-[111, 112, 114, 118, 119, 121, 122, 124]

 type(list): For the type() function, it returns the class type of an object

Eg.

List=[111,112,114,121,122,124,118,119]

type(List)

 append():Used for appending and adding elements to List. It is used to


add elements to the last position of the List.

Eg.

List=[111,112,114,121,122,124,118,119]

List.append([456,345,9809])

Output-[111, 112, 114, 121, 122, 124, 118, 119, [456, 345, 9809]]

 extend(): The extend() method increases the length of the list by the
number of elements that are provided to the strategy, so if you’d prefer
to add multiple elements to the list, you will be able to use this method.

Eg.
FUNCTION OF LIST
List=[111,112,114,121,122,124,118,119]

List.extend([453,333,333,478])

print(List)

output-[111, 112, 114, 121, 122, 124, 118, 119, 453, 333, 333, 478]

index(): Returns the first appearance of a particular value.

Eg.

List=[111,112,114,121,122,124,118,119]

List.index(114)

 max(list): The max() function will return the highest value of the inputted
values

Eg.

List=[111,112,114,121,122,124,118,119]

max_list=max(List)

print(max_list)

output-124

 min(list): The min() function will return the lowest value of the inputted
values.

Eg.

List=[111,112,114,121,122,124,118,119]

min_list=min(List

print(min_list)

output-111
FUNCTION OF LIST
 len(list): The len() function shows the number of elements in a list.

Eg.

List=[111,112,114,121,122,124,118,119]

List2=[111,112,114,121,122,124,118,119,56,90,24]

print(len(List))

print(len(List2))

output-

11

 clear(): Removes all the elements from the list.

List=[111,112,114,121,122,124,118,119]

Eg.

List.clear()

print(List)

output-[]

 insert(): Inserts an element at the specified position. .

Eg.

List=[111,112,114,121,122,124,118,119]

List.insert(5,[45,67,89])

print(List)

output-[111, 112, 114, 121, 122, [45, 67, 89], 124, 118, 119]

 count(): Returns the number of elements with the required value.

Eg.
FUNCTION OF LIST
List=[111,112,114,121,122,124,118,119,119,111]

v=List.count(111)

print(v)

output-2

 pop(): Removes the element at the required position.

Eg.

List=[111,112,114,121,122,124,118,119,119,111]

List.pop(1)

print(List)

output-

[111, 114, 121, 122, 124, 118, 119, 119, 111]

 remove(): Removes the primary item with the desired value.

Eg.

List=[111,112,114,121,122,124,118,119,119,111]

List.remove(111)

print(List)

 reverse(): Reverses the order of the list.

Eg.

List=[111,112,114,121,122,124,118,119,119]

List.reverse()

print(List)

output-[119, 119, 118, 124, 122, 121, 114, 112, 111]


FUNCTION OF LIST
 copy(): Returns a duplicate of the list.

List=[111,112,114,121,122,124,118,119,119,111]

list2=List.copy()

print(list2)

output-[111, 112, 114, 121, 122, 124, 118, 119, 119, 111]

You might also like