List Functions in Python

 

List Functions in Python

Python has a number of built-in functions that can be used on lists that allow you to quickly look through a list without writing your own loops. The following are the functions on the list.

len
max
min
sum
split
join

Video Tutorial

The len function returns the number of elements in the list. The min and max functions return the minimum and maximum elements of a list.

A simple program to demonstrate the list functions len, min, max

#Lists and functions such as len, min, max and sum

nums = [3, 41, 12, 9, 70, 15]

print("The lenth of the list is: ", len(nums))

print("Maximum Element is: ", max(nums))

print("Minimum Element is: ", min(nums))

print("Sum of Elements is: ", sum(nums))

print("The average of list is: ", sum(nums)/len(nums))

Output

The Lenth of the list is: 6

Maximum Element is: 70

Minimum Element is: 3

Sum of Elements is: 150

The average of list is: 25.0

split function in python

The split function in python is used to divide the string into a list of elements. By default, the delimiter for dividing the string into a list is “space”. If you want to specify any other delimiter, it should be passed as an argument to split function explicitly. The following program demonstrates the usage of the split method on the list in python.

s = 'welcome to CSE VTU BGM'
t = s.split()
print(t)

Output

[‘welcome’, ‘to’, ‘CSE’, ‘VTU’, ‘BGM’]

Another example with explicit delimiter ::

s = 'spam::spam::spam'
delimiter = '::'
t = s.split(delimiter)
print (t)

Output

[‘spam’, ‘spam’, ‘spam’]

Another example with explicit delimiter ,

s = 'spam,spam,spam'
delimiter = ','
t = s.split(delimiter)
print (t)

Output

[‘spam’, ‘spam’, ‘spam’]

join function in python

The join function is used to join the list elements to form the string. The following program demonstrates the usage of the join method on the list in python.

t = ['Welcome', 'to', 'CSE', 'VTU', 'BGM']
delimiter = '::'
s = delimiter.join(t)
print (s)

Output

Welcome::to::CSE::VTU::BGM

Summary:

This tutorial discusses the usage of List functions in python. If you like the tutorial do share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *