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

Python List Methods Explained with Examples

This document provides a comprehensive overview of Python list methods, detailing their functionalities with examples and expected outputs. Key methods include append, extend, insert, remove, pop, clear, index, count, sort, reverse, and copy. Each method is illustrated with a code example and its resulting output.

Uploaded by

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

Python List Methods Explained with Examples

This document provides a comprehensive overview of Python list methods, detailing their functionalities with examples and expected outputs. Key methods include append, extend, insert, remove, pop, clear, index, count, sort, reverse, and copy. Each method is illustrated with a code example and its resulting output.

Uploaded by

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

# Python List Methods (All) with Examples and Outputs

1. **append(x)**

Adds an item to the end.

Example:

a = [1, 2]

[Link](3)

Output: [1, 2, 3]

2. **extend(iterable)**

Adds all elements from another iterable.

Example:

a = [1, 2]

[Link]([3, 4])

Output: [1, 2, 3, 4]

3. **insert(i, x)**

Inserts at index.

Example:

a = [1, 3]

[Link](1, 2)

Output: [1, 2, 3]

4. **remove(x)**

Removes first occurrence of x.

Example:

a = [1, 2, 2, 3]

[Link](2)
Output: [1, 2, 3]

5. **pop(i=-1)**

Removes and returns item at index.

Example:

a = [10, 20, 30]

x = [Link]()

Output: [10, 20] 30

6. **clear()**

Clears the list.

Example:

a = [1, 2, 3]

[Link]()

Output: []

7. **index(x)**

Returns index of the first occurrence.

Example:

a = ['a', 'b', 'c']

Output: 1

8. **count(x)**

Counts occurrences.

Example:

a = [1, 1, 2]

Output: 2

9. **sort()**
Sorts the list.

Example:

a = [3, 1, 2]

[Link]()

Output: [1, 2, 3]

10. **reverse()**

Reverses the list.

Example:

a = [1, 2, 3]

[Link]()

Output: [3, 2, 1]

11. **copy()**

Returns a shallow copy.

Example:

a = [1, 2]

b = [Link]()

Output: [1, 2]

You might also like