Basic List Operations in Python

Lists are sequence objects in Python that support the same fundamental operations as strings. They respond to the + and * operators for concatenation and repetition, except the result is a new list rather than a string.

Lists support all general sequence operations, making them versatile for data manipulation and processing tasks.

Length Operation

Use len() to get the number of elements in a list ?

numbers = [1, 2, 3]
print("Length of list:", len(numbers))
Length of list: 3

Concatenation with + Operator

Combine two lists using the + operator ?

list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print("Concatenated list:", combined)
Concatenated list: [1, 2, 3, 4, 5, 6]

Repetition with * Operator

Repeat list elements using the * operator ?

greeting = ['Hi!']
repeated = greeting * 4
print("Repeated list:", repeated)
Repeated list: ['Hi!', 'Hi!', 'Hi!', 'Hi!']

Membership Testing with in Operator

Check if an element exists in a list using the in operator ?

numbers = [1, 2, 3]
result = 3 in numbers
print("Is 3 in the list?", result)
print("Is 5 in the list?", 5 in numbers)
Is 3 in the list? True
Is 5 in the list? False

Iteration with for Loop

Loop through list elements using a for loop ?

numbers = [1, 2, 3]
print("Iterating through list:")
for x in numbers:
    print(x, end=" ")
Iterating through list:
1 2 3 

Summary of Basic Operations

Python Expression Results Description
len([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation
['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition
3 in [1, 2, 3] True Membership
for x in [1, 2, 3]: print(x) 1 2 3 Iteration

Conclusion

Python lists support all fundamental sequence operations including length, concatenation, repetition, membership testing, and iteration. These operations make lists powerful and flexible for handling collections of data in your programs.

Updated on: 2026-03-25T07:34:06+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements