List
List
on it. Here are some common operations and examples of how you might work with such a list:
You can access elements of the list using their index. Python lists are zero-indexed, so the first
element is at index 0.
```python
print(a[2]) # Output: 10
```
You can loop through the list to access or modify its elements.
```python
for item in a:
print(item)
```
You can add new elements to the end of the list using the `append()` method.
```python
a.append("new_item")
```
### 4. **Inserting Elements**
You can insert elements at a specific position using the `insert()` method.
```python
a.insert(2, "inserted_item")
```
You can remove elements by value using the `remove()` method or by index using the `pop()`
method.
```python
a.remove(10)
removed_item = a.pop(1)
```
You can find the index of a specific element using the `index()` method.
```python
index_of_item = a.index("a")
print(index_of_item) # Output: 0
```
```python
```
You can check if an element is present in the list using the `in` operator.
```python
if 10 in a:
else:
```
You can sort the list if it contains elements of a comparable type. If the list contains mixed types (like
strings and integers), sorting might raise an error.
```python
# This will raise an error because the list contains different data types.
# a.sort()
# If you want to sort a list with similar types only, you can use:
string_list.sort()
```
```python
```
If you need to handle or process lists with mixed data types, you often need to account for the
different types of elements.
```python
```
Feel free to ask if you have specific operations or transformations in mind for your list!