Singapore 640447
+91-9677454551 | +65-83866984
amsavallimylasalam@gmail.com

Learn The Fundamentals Of Python List Functions in 3 Minutes

Evolving Data Scientist

Learn The Fundamentals Of Python List Functions in 3 Minutes

In Python a List is an Inbuilt data structure a mutable ,ordered sequence of items.

Once list is created ,We can modify ,replace and changed the order of items.

List Functions in Python

Creating a list

Each element or value inside a list is known as an Item.Each item in a list separated by a comma and enclosed within square brackets[].

Here We are creating a Programming list.

## List of programming
programming=[‘Python’,’Java’,’Javascript’,’PHP’]
programming

Output:

['Python', 'Java', 'Javascript', 'PHP']

Add an item to the list: append()

Here We are adding Oracle to the programming list with the help of append().

programming.append('Oracle')
programming

Output

['Python', 'Java', 'Javascript', 'PHP', 'Oracle']

Remove an item from the List: remove()

In this method We are removing single item (i.e Javascript) from the list.

remove() function used to remove a single item from the list.

programming.remove(‘Javascript’)
programming

Output:

['Python', 'Java', 'PHP', 'Oracle']

Python List index()

The index() method gives the index of the particular element in the list.

programming.index('PHP')

Output:

2

Total Items in the List

Through len() function we can get the total number of items in a list.

len(programming)

Output:

4

List Functions

Marks=[490,430,492,380]
Marks

Output:

[490, 430, 492, 380]

sort()

The sort() method sorts the list ascending by default.

Marks.sort()

Output:

[380, 430, 490, 492]

Sort the list in descending order

By setting reverse=True as an argument in sort() We can sort out the list in descending order.

Marks=[380, 430, 490, 492]Marks.sort(reverse=True)
Marks

Output:

[492, 490, 430, 380]

python list copy()

Copy() function gives new list but it doesn’t modify original list.Here flowers list is copied to fragrance list.

flowers=[‘Rose’,’Jasmine’,’Lily’]
fragrance=flowers.copy()
print(fragrance)

Output:

['Rose', 'Jasmine', 'Lily']

List insert() function

The insert() function inserts the required items at the desired position.

flowers=[‘Rose’, ‘Jasmine’, ‘Lily’]flowers.insert(2,’Lotus’)
flowers

Output:

['Rose', 'Jasmine', 'Lotus', 'Lotus', 'Lily']

List extend() Method

extend() Method add the specified elements to the end of the list.Here we are extending list1 with list2.

list1=[1,2,3,4,5]
list2=[6,7,8,9,10]
list1.extend(list2)
print(list1)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

list.pop(index)

The pop() method removes the specified items through given index from the list.Here the index of pop(0) that is item 1 from the list1 is pop out.

list1=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

list1.pop(0)
list1

Output:

[2, 3, 4, 5, 6, 7, 8, 9, 10]

Reverse the element of the list

By reverse() function we can reverse the element of the list.

list1.reverse()
list1

Output:

[10, 9, 8, 7, 6, 5, 4, 3, 2]

max() function

max() function will gives the highest value from the list.

max(list1)

Output:

10

min()

min() function will gives the minimum value from the list.

min(list1)

Output:

2

List clear() Function

The clear() function removes all items in the list and make it as an empty list.

even=[2,4,6,8]
even.clear()
even

Output:

[]

Conclusion

In this article you gain an understanding of List and working with list function as well as covered the some important characteristics of list in python.

Leave a Reply

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