List in Python


A list in Python is a built-in, ordered, and mutable collection of elements. It can hold items of different data types such as integers, strings, floats, or even other lists.

Key Features:
Ordered: Items maintain their insertion order.

Mutable: You can add, remove, or change items after the list is created.

Heterogeneous: Can store mixed data types.

Indexable: Elements can be accessed using indices starting from 0.

# Declaring list
list1 = []

# Adding elements to list
list1.append('a')
list1.append('b')
list1.append('c')
list1.append(2)

print(list1)
# Output -> ['a', 'b', 'c', 2]

length_of_list1=len(list1)
print(length_of_list1)

# Output
# 4

# Print the list elements one by one using their indices:
print(list1[0])
print(list1[1])
print(list1[2])
print(list1[3])

# Output ->
# a
# b
# c
# 2

# Print the list elements using for loop:
for i in range(0, 4):
    print(list1[i])

# Output ->
# a
# b
# c
# 2

list1.remove('b')
print(list1)

# Output
# ['a', 'c', 2]