Home » All Blogs » Artificial intelligence » M.Sc AI Practical Assignment Solution 2

M.Sc AI Practical Assignment Solution 2

Question:

Q2 Write a Python program to implement List Operations (Nested list, Length, Concatenation, Membership, Iteration, Indexing and Slicing), List Methods (Add, Append, Extend & Delete)

Code:

# Nested List
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Length of a list
length = len(nested_list)
print("Length of the list:", length)

# Concatenation of lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print("Concatenated list:", concatenated_list)

# Membership check
check = 3 in concatenated_list
print("Membership check:", check)

# Iteration over a list
print("Iteration over the list:")
for item in concatenated_list:
    print(item)

# Indexing and slicing
list3 = [7, 8, 9, 10, 11]
print("Indexing and slicing:")
print("First element:", list3[0])
print("Last element:", list3[-1])
print("Sliced list:", list3[1:4])

# List methods
my_list = [1, 2, 3]

# Add an element at a specific index
my_list.insert(1, 4)
print("After inserting 4 at index 1:", my_list)

# Append an element at the end
my_list.append(5)
print("After appending 5 at the end:", my_list)

# Extend the list with another list
my_list.extend([6, 7, 8])
print("After extending the list:", my_list)

# Delete elements from the list
my_list.remove(2)
print("After removing 2 from the list:", my_list)

del my_list[0]
print("After deleting the first element:", my_list)

Output:

  
Tech Amplifier Final Logo