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

List Operations in Python

The document provides a comprehensive overview of basic list operations in Python, including creating, accessing, modifying, and removing elements. It covers methods for appending, inserting, slicing, and checking membership, as well as sorting and reversing lists. Additionally, it explains list concatenation, repetition, copying, and clearing lists.

Uploaded by

Shruti Shevade
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)
3 views2 pages

List Operations in Python

The document provides a comprehensive overview of basic list operations in Python, including creating, accessing, modifying, and removing elements. It covers methods for appending, inserting, slicing, and checking membership, as well as sorting and reversing lists. Additionally, it explains list concatenation, repetition, copying, and clearing lists.

Uploaded by

Shruti Shevade
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
You are on page 1/ 2

Basic List Operations in Python

Creating a List:

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

Accessing Elements:

print(my_list[0]) # First element

print(my_list[-1]) # Last element

Modifying Elements:

my_list[1] = 20 # Changes second element to 20

Appending Elements:

my_list.append(6) # Adds 6 at the end

Inserting Elements:

my_list.insert(2, 15) # Inserts 15 at index 2

Removing Elements:

my_list.remove(3) # Removes first occurrence of 3

Popping Elements:

my_list.pop() # Removes and returns last element

Slicing a List:

print(my_list[1:4]) # Elements from index 1 to 3

Length of List:

print(len(my_list)) # Number of elements

List Concatenation:

new_list = my_list + [7, 8]

List Repetition:

repeated = my_list * 2
Checking Membership:

print(3 in my_list) # True if 3 is in the list

Iterating Through a List:

for item in my_list:

print(item)

Sorting a List:

my_list.sort() # Sorts the list in ascending order

Reversing a List:

my_list.reverse() # Reverses the list order

Copying a List:

copy_list = my_list.copy()

Clearing a List:

my_list.clear() # Removes all elements

You might also like