0% found this document useful (0 votes)
4 views2 pages

Essential Python List Functions Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Essential Python List Functions Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Matric number: 20/85/0040

Course code: COM 315

Course title: Python programming language

There are several library functions in python list and explain at least five functions with a short simple
code

1. `len()`: Returns the length of a list.

Example: `my_list = [1, 2, 3, 4, 5]`

`print(len(my_list))`

Output: `5`

2. `append()`: Adds an element to the end of a list.

Example: `my_list = [1, 2, 3]`

`my_list.append(4)`

`print(my_list)`

Output: `[1, 2, 3, 4]`

3. `remove()`: Removes the first occurrence of a specified element from a list.

Example: `my_list = [1, 2, 3, 4, 5]`

`my_list.remove(3)`

`print(my_list)`

Output: `[1, 2, 4, 5]`

4. `sort()`: Sorts the elements in a list in ascending order.

Example: `my_list = [5, 3, 1, 4, 2]`

`my_list.sort()`
`print(my_list)`

Output: `[1, 2, 3, 4, 5]`

5. `index()`: Returns the index of the first occurrence of a specified element in a list.

Example: `my_list = [1, 2, 3, 4, 5]`

`print(my_list.index(3))`

Output: `2`

You might also like