0% found this document useful (0 votes)
3 views

# Lists in Python

Lists in Python are versatile, ordered, mutable collections that can hold heterogeneous elements. They can be created, accessed, modified, and manipulated using various methods and operations. Understanding lists is essential for effective programming in Python.

Uploaded by

MUKUL CHAUHAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

# Lists in Python

Lists in Python are versatile, ordered, mutable collections that can hold heterogeneous elements. They can be created, accessed, modified, and manipulated using various methods and operations. Understanding lists is essential for effective programming in Python.

Uploaded by

MUKUL CHAUHAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# Lists in Python

Lists are one of Python's most versatile and commonly used data structures. They are ordered,
mutable collections that can hold elements of different data types.

## Characteristics of Lists

1. **Ordered**: Elements maintain their insertion order


2. **Mutable**: Can be modified after creation
3. **Heterogeneous**: Can contain different data types
4. **Dynamic**: Can grow or shrink as needed
5. **Indexable**: Elements accessible via zero-based index
6. **Allow duplicates**: Can contain duplicate values

## Creating Lists

```python
# Empty list
empty_list = []

# List with elements


numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
nested = [[1, 2], [3, 4], [5, 6]]

# Using list constructor


from_range = list(range(5)) # [0, 1, 2, 3, 4]
from_string = list("hello") # ['h', 'e', 'l', 'l', 'o']
```

## Accessing Elements

```python
fruits = ['apple', 'banana', 'cherry', 'date']

# Positive indexing (0-based)


print(fruits[0]) # 'apple'
print(fruits[2]) # 'cherry'

# Negative indexing (starts from end)


print(fruits[-1]) # 'date'
print(fruits[-2]) # 'cherry'

# Slicing [start:stop:step]
print(fruits[1:3]) # ['banana', 'cherry']
print(fruits[::2]) # ['apple', 'cherry']
print(fruits[::-1]) # ['date', 'cherry', 'banana', 'apple'] (reverse)
```

## Modifying Lists

```python
colors = ['red', 'green', 'blue']

# Change element by index


colors[1] = 'yellow' # ['red', 'yellow', 'blue']

# Append (add to end)


colors.append('green') # ['red', 'yellow', 'blue', 'green']

# Insert at specific position


colors.insert(1, 'orange') # ['red', 'orange', 'yellow', 'blue', 'green']

# Remove elements
colors.remove('yellow') # Removes first occurrence
popped = colors.pop(2) # Removes and returns item at index 2
del colors[0] # Removes item at index 0
```

## Common Operations

```python
a = [1, 2, 3]
b = [4, 5, 6]

# Concatenation
combined = a + b # [1, 2, 3, 4, 5, 6]

# Repetition
repeated = a * 2 # [1, 2, 3, 1, 2, 3]

# Membership testing
print(2 in a) # True
print(7 not in a) # True

# Length
print(len(a)) #3
```
## List Methods

| Method | Description | Example |


|--------|-------------|---------|
| `append(x)` | Adds x to end | `lst.append(4)` |
| `extend(iterable)` | Adds all elements | `lst.extend([4,5])` |
| `insert(i, x)` | Inserts x at i | `lst.insert(1, 'x')` |
| `remove(x)` | Removes first x | `lst.remove(2)` |
| `pop([i])` | Removes/returns at i | `x = lst.pop()` |
| `clear()` | Removes all | `lst.clear()` |
| `index(x)` | Returns index of x | `i = lst.index(3)` |
| `count(x)` | Counts occurrences | `c = lst.count(2)` |
| `sort()` | Sorts in place | `lst.sort()` |
| `reverse()` | Reverses in place | `lst.reverse()` |
| `copy()` | Shallow copy | `new = lst.copy()` |

## List Comprehensions

Concise way to create lists:

```python
# Squares of numbers 0-9
squares = [x**2 for x in range(10)]

# Even numbers from existing list


evens = [x for x in numbers if x % 2 == 0]

# Nested comprehension
matrix = [[i*j for j in range(3)] for i in range(4)]
```

Lists are fundamental to Python programming and understanding them thoroughly will make you
more effective at working with Python data structures.

You might also like