0% found this document useful (0 votes)
15 views5 pages

List Functions

Lists are a commonly used data structure in Python. This document explores essential list functions like len(), append(), extend(), insert(), remove(), pop(), clear(), index(), count(), sort(), reverse(), and copy(). These functions allow adding, removing, searching, and sorting elements in lists efficiently. List functions modify lists in-place by default, so it's important to understand how they impact the original list.

Uploaded by

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

List Functions

Lists are a commonly used data structure in Python. This document explores essential list functions like len(), append(), extend(), insert(), remove(), pop(), clear(), index(), count(), sort(), reverse(), and copy(). These functions allow adding, removing, searching, and sorting elements in lists efficiently. List functions modify lists in-place by default, so it's important to understand how they impact the original list.

Uploaded by

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

LIST FUNCTIONS

Lists are one of the most versatile and commonly used data structures in Python.
They are used to store collections of items, and Python provides various built-in
functions to manipulate and interact with lists efficiently. In this guide, we will
explore the essential list functions in Python and learn how to use them
effectively.

len() - Returns the length of the list


The len() function returns the number of elements present in a list. It is a built-
in function and works with other Python data structures like strings, tuples, etc.

Syntax: python length = len(list)

Example:
Code

script.py
1
2
fruits = ['apple', 'banana', 'orange', 'kiwi']
print(len(fruits)) # Output: 4

Execute code

append() - Adds an element to the end of the list


The append() function allows you to add an element to the end of the list. It
modifies the original list in place.

Syntax: python list.append(element)

Example:
Code

script.py
1
2
3
numbers = [1, 2, 3]
numbers.append(4)
print(numbers) # Output: [1, 2, 3, 4]

Execute code

extend() - Appends elements from another list to the current list


The extend() function adds elements from another list to the end of the current
list. It extends the list in place.

Syntax: python list.extend(iterable)

Example:
Code

script.py
1
2
3
4
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1) # Output: [1, 2, 3, 4, 5, 6]

Execute code

insert() - Inserts an element at a specific index in the list


The insert() function allows you to insert an element at a specific index in the
list. It shifts the existing elements to the right.

Syntax: python list.insert(index, element)

Example:
Code

script.py
1
2
3
fruits = ['apple', 'banana', 'kiwi']
fruits.insert(1, 'orange')
print(fruits) # Output: ['apple', 'orange', 'banana', 'kiwi']

Execute code

remove() - Removes the first occurrence of a specific element


The remove() function is used to remove the first occurrence of a specified element
from the list.

Syntax: python list.remove(element)

Example:
Code

script.py
1
2
3
numbers = [1, 2, 3, 4, 2]
numbers.remove(2)
print(numbers) # Output: [1, 3, 4, 2]

Execute code

pop() - Removes and returns the element at a specific index


The pop() function is used to remove and return the element at a specific index. If
no index is provided, it removes and returns the last element.

Syntax: python element = list.pop([index])

Example:
Code

script.py
1
2
3
4
fruits = ['apple', 'banana', 'orange']
removed_fruit = fruits.pop(1)
print(removed_fruit) # Output: 'banana'
print(fruits) # Output: ['apple', 'orange']

Execute code

clear() - Removes all elements from the list


The clear() function removes all elements from the list, making it an empty list.

Syntax: python list.clear()

Example:
Code

script.py
1
2
3
numbers = [1, 2, 3, 4]
numbers.clear()
print(numbers) # Output: []

Execute code

index() - Returns the index of the first occurrence of a specific element


The index() function returns the index of the first occurrence of a specified
element in the list.

Syntax: python index = list.index(element)

Example:
Code

script.py
1
2
3
fruits = ['apple', 'banana', 'kiwi', 'banana']
index = fruits.index('banana')
print(index) # Output: 1

Execute code

count() - Returns the number of occurrences of a specific element


The count() function returns the number of occurrences of a specified element in
the list.

Syntax: python count = list.count(element)

Example:
Code
script.py
1
2
3
fruits = ['apple', 'banana', 'kiwi', 'banana']
count = fruits.count('banana')
print(count) # Output: 2

Execute code

sort() - Sorts the list in ascending order


The sort() function is used to sort the list in ascending order. It modifies the
original list in place.

Syntax: python list.sort()

Example:
Code

script.py
1
2
3
numbers = [3, 1, 2, 5, 4]
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4, 5]

Execute code

reverse() - Reverses the order of elements in the list


The reverse() function reverses the order of elements in the list. It modifies the
original list in place.

Syntax: python list.reverse()

Example:
Code

script.py
1
2
3
fruits = ['apple', 'banana', 'kiwi']
fruits.reverse()
print(fruits) # Output: ['kiwi', 'banana', 'apple']

Execute code

copy() - Creates a shallow copy of the list


The copy() function creates a shallow copy of the list. It does not copy the
elements within nested lists, only the references.

Syntax: python new_list = list.copy()

Example:
Code

script.py
1
2
3
numbers = [1, 2, 3]
numbers_copy = numbers.copy()
print(numbers_copy) # Output: [1, 2, 3]

Execute code

Conclusion
List functions in Python provide a powerful set of tools for manipulating and
managing lists. Understanding these functions allows you to efficiently work with
lists and perform various operations such as adding, removing, searching, and
sorting elements.

Remember that lists are mutable, meaning you can modify them directly using these
functions. Keep in mind the differences between modifying a list in place and
creating a new list to ensure the desired behavior in your code.

You might also like