Python List Methods And Functions

Python List Methods And Functions

In this post, we will learn Python List Methods And Functions.

First, let’s see all the Python List Methods And Functions, in brief, using the table given below. Here, the methods/functions name is written with their brief description.


MethodUse
len()returns list length
sort()sorts the list
min()returns the minimum value from list
max()returns the maximum value from list
list()converts a sequential data type to list
append()adds an element at the end of the list
clear()removes all elements from the list
copy()returns the copied list
reverse()reverses the order of the elements of the list
sum()returns the sum of the elements of the list
range()returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number
pop()removes the element at the specified position
remove()removes the item with the specified value
index()returns the index of the first element with the specified value
insert()Adds an element at the specified position

The len() function


Using len() function, we can find the length of a list i.e. the number of elements present in the list.

Example

myList = [34, 12, 10, 50, 45, 13]
print(len(myList))

Output

6

The sort() method

Using the sort() method, we can sort any list but the elements in the list should be of the same datatypes like int or string.

Example

myList = [34, 12, 10, 50, 45, 13]
myList.sort()
print(myList)
myList2 = ["d", "c", "a", "b", "B", "A"]
myList2.sort()
print(myList2)

Output

[10, 12, 13, 34, 45, 50]
['A', 'B', 'a', 'b', 'c', 'd']


The min() method

Using the min() method, we can find the minimum value from the list but the elements in the list should be of the same datatypes like int or string.

Example

myList = [34, 12, 10, 50, 45, 13]
print(min(myList))
myList1 = ["d", "c", "a", "b", "B", "A"]
print(min(myList1))

Output

10
A

The max() method

Using the max() method, we can find the maximum value from the list but the elements in the list should be of the same datatypes like int or string.

Example

myList = [34, 12, 10, 50, 45, 13]
print(max(myList))
myList1 = ["d", "c", "a", "b", "B", "A"]
print(max(myList1))

Output

50
d

The list() method

Using the list() method, we can convert any sequence datatype into lists. Datatypes like Tuples, Strings, Sets, etc can be converted to Python Lists.

Example

myTuple = (1, 2, 3, 4)
myList1 = list(myTuple)
myString = "Hello"
myList2 = list(myString)
mySet = {1, 2, 3, 4}
myList3 = list(mySet)
print(myList1)
print(myList2)
print(myList3)

Output

[1, 2, 3, 4]
['H', 'e', 'l', 'l', 'o']
[1, 2, 3, 4]

The append() method

Using the append() method, we can add an element at the end of a Python List.

Example

myList = [1, 2, 3, 4]
myList.append(5)
print(myList)

Output

[1, 2, 3, 4, 5]

The clear() method

Using the clear() method, we can clear all elements from a Python List i.e. we can delete all elements.

Example

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

Output

[]


The copy() method

Using the copy() method, we can copy one Python list to another Python list.

Example

myList1 = [1, 2, 3, 4]
myList2 = myList1.copy()
print(myList2)

Output

[1, 2, 3, 4]

Note:- You can not copy a list just by using the “=” operator as it will be called as assigning one list to another not copying. If you do that, then any changes you will make in the first list will also apply to the second list.

Example

myList1 = [1, 2, 3, 4]
myList2 = myList1  # referring one list to another
print(myList1)
print(myList2)
myList1.append(5)  # appending 5 in myList1
print(myList1)
print(myList2)  # changes will appear in myList2 too

Output

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

The reverse() method

Using the reverse() method, we can reverse the order of the position of elements in a Python List.

Example

myList = [1, 2, 3, 4]
myList.reverse()
print(myList)

Output

[4, 3, 2, 1]

The sum() function

Using the sum() function, we can get the sum of Python’s iterable objects like Lists, Tuple, and dictionary. It takes two arguments, first one is the iterable object and the second one is the number you want to add with the numbers present in the iterable object. The sum() function adds only numbers else it will give an error.

Example

myList = [1, 2, 3, 4, 5, 6, 7]
addition1 = sum(myList)
print(addition1)
addition2 = sum(myList, 3)
print(addition2)

Output

28
31

The range() function

Using the range() function, we can get a list that contains a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before the specified number, since using the range() function we can create a list of the desired sequence which will save our time and work.

Example

myList1 = [range(15)]
myList2 = [range(4, 15)]
myList3 = [range(4, 15, 3)]
print(myList1)
print(myList2)
print(myList3)

Output

[range(0, 15)]
[range(4, 15)]
[range(4, 15, 3)]

Surprised, yes this is not we wanted, actually, we need to unpack the result of the range() function using the argument-unpacking operator i.e. *.

Example

myList1 = [*range(15)]
myList2 = [*range(4, 15)]
myList3 = [*range(4, 15, 3)]
print(myList1)
print(myList2)
print(myList3)

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[4, 7, 10, 13]

The pop() function

Using the pop() function, we can remove the last value of the list. If we want to remove any other value from the list using the pop() function, then we need to pass the index number to the pop() function, else the last value will be removed. The pop() function always returns the removed value.

Example

myList = [4, 5, 6, 7, 8, 9, 10, 11] 
print(myList.pop())  # 11
print(myList)
print(myList.pop(3))  # 7
print(myList)
print(myList.pop(0))  # 4
print(myList)
print(myList.pop())  # 10
print(myList)

Output

11
[4, 5, 6, 7, 8, 9, 10]
7
[4, 5, 6, 8, 9, 10]
4
[5, 6, 8, 9, 10]
10
[5, 6, 8, 9]


The remove() function

Using the remove() function, we can remove any element from the list. To remove an element, pass the element as an argument to the function. The remove() function does not return any value and gives an error if the value is not present in the list.

Example

myList = [4, 5, 6, 7, 8, 9, 10, 11]
print(myList.remove(4))  # removes 4 and returns nothing
print(myList)
myList.remove(7)
print(myList)
myList.remove(11)
print(myList)
print(myList.remove(3))  # 3 not present

Output

None
[5, 6, 7, 8, 9, 10, 11]
[5, 6, 8, 9, 10, 11]
[5, 6, 8, 9, 10]
Traceback (most recent call last):
  File "main.py", line 8, in 
    print(myList.remove(3))  # 3 not present
ValueError: list.remove(x): x not in list

What if one value is repeated? In that case, the remove() function removes the first one only and leaves other values at the same place where they were before.

Example

myList = [4, 5, 6, 7, 4, 8, 9, 4, 10, 11]
myList.remove(4)
print(myList)

Output

[5, 6, 7, 4, 8, 9, 4, 10, 11]

Now, let’s see one-liner to remove all the similar elements from the list.

Example

myList = [4, 5, 6, 7, 4, 8, 9, 4, 10, 11]
while 4 in myList: myList.remove(4)
print(myList)

Output

[5, 6, 7, 8, 9, 10, 11]

The index() function

Using the index() function, we can find/search for an element in any list. As soon as the element is found, it returns the index of the element. If there are two or more elements with similar values in the list, it will return the index of the element with the lowest index number.

We can pass two more arguments to the index() function, start(from where we want to start searching the element in the list), and end(where we want to end search the element in the list). These two arguments are optional.

Syntax:

listname.index(element, start, end)

Now, let’s see an example

Example

myList = [4, 5, 6, 7, 4, 8, 9]
print(myList.index(5))
print(myList.index(4))
print(myList.index(4, 2, 5))
print(myList.index(4, 2, 3))

Output

1
0
4
Traceback (most recent call last):
  File "main.py", line 5, in 
    print(myList.index(4, 2, 3))
ValueError: 4 is not in list

The insert() function

Using the insert() function, we can insert an element in the list on our desired location/index.

Syntax:

listname.insert(index, element)

If the index value is greater than or equal to the len(listname), then the element is inserted at the end of the list. It does not return any value and gives an error if it is used with other objects instead of the list.

Example

myList = [4, 5, 6, 7, 4, 8, 9]
myList.insert(0, 1)
print(myList)
myList.insert(1, 2)
print(myList)
myList.insert(10, 12)
print(myList)

Output

[1, 4, 5, 6, 7, 4, 8, 9]
[1, 2, 4, 5, 6, 7, 4, 8, 9]
[1, 2, 4, 5, 6, 7, 4, 8, 9, 12]

For more, you can check to official Python Data Structures, Click here.


Read More:


Get Jokes with Python

Python from Scratch

Calculator Program In Python

Snake Game in Python using Pygame

Covid-19 Tracker Application Using Python

YouTube Video Downloader Application Using Python

GUI Application To See wifi password in Python


Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you