list in python
list in python
A list is a versatile and widely used data structure in Python. It is a mutable, ordered, and
iterable collection that can store elements of any data type.
1. Ordered
o Lists maintain the order of the elements.
o Example:
2. Mutable
Lists can be modified after creation. You can add, remove, or change elements.
Example
my_list = [1, 2, 3]
my_list[1] = 20
3. Heterogeneous
A list can contain elements of different data types (e.g., integers, strings, floats, etc.).
Example:
my_list = [1, "hello", 3.14]
4. Dynamic
Lists are dynamic in size, meaning you can add or remove elements at any time.
Example:
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
5. Indexed
Lists use zero-based indexing, allowing direct access to elements.
my_list = [10, 20, 30]
print(my_list[1]) # Output: 20
6. Supports Negative Indexing
you can access elements using negative indices, where -1 refers to the last element, -2 to
the second last, and so on.
7. Supports Nesting
8. Iterable
Lists can be iterated over using loops, comprehensions, or other iterable operations.
my_list = [1, 2, 3]
for x in my_list:
print(x)
# Output: 1 2 3
9. Supports Slicing
my_list = [0, 1, 2, 3, 4]
Advantages of Lists
Ease of Use: Lists provide simple syntax for operations like addition, deletion, and
iteration.
Flexibility: They can store any data type, including mixed types and nested structures.
Rich Functionality: With built-in methods and list comprehensions, lists are highly
versatile for a wide range of use cases.
Accessing Elements
a. Indexing (Zero-based)
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # Output: 10
print(my_list[3]) # Output: 40
b. Negative Indexing
print(my_list[-1]) # Output: 50 (last element)
print(my_list[-3]) # Output: 30 (third element from the end)
c. Slicing
print(my_list[1:4]) # Output: [20, 30, 40]
print(my_list[:3]) # Output: [10, 20, 30]
print(my_list[::2]) # Output: [10, 30, 50] (step of 2)
2. Adding Elements
a. Append an Element
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
b. Insert an Element
my_list.insert(1, 100)
print(my_list) # Output: [1, 100, 2, 3, 4]
c. Extend a List
my_list.extend([5, 6])
print(my_list) # Output: [1, 100, 2, 3, 4, 5, 6]
3. Removing Elements
a. Remove an Element
my_list.remove(100)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
b. Pop an Element
Removes and returns the element at a specific index (default: last element).
last_element = my_list.pop()
print(last_element) # Output: 6
print(my_list) # Output: [1, 2, 3, 4, 5]
my_list.clear()
print(my_list) # Output: []
4. Modifying Elements
a. Replace an Element
my_list = [1, 2, 3, 4, 5]
my_list[2] = 300
print(my_list) # Output: [1, 2, 300, 4, 5]
a. Sort a List
my_list = [5, 2, 9, 1]
my_list.sort()
print(my_list) # Output: [1, 2, 5, 9]
c. Reverse a List
my_list.reverse()
print(my_list) # Output: [1, 2, 5, 9] (reversed order)
6. Copying a List
original_list = [1, 2, 3]
copied_list = original_list.copy()
print(copied_list) # Output: [1, 2, 3]
7. Mathematical Operations
a. Sum of Elements
my_list = [1, 2, 3, 4]
print(sum(my_list)) # Output: 10
8. Membership Testing
a. Using Loops
for item in my_list:
print(item)
b. List Comprehension
squares = [x**2 for x in my_list]
print(squares) # Output: [1, 4, 9, 16]
Output:
[5, 4, 3, 2, 1]
The reverse=True parameter tells Python to sort the list in descending
order.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(my_list))
print("List after removing duplicates:", unique_list)