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

Python List Full Notes

A Python list is a mutable, ordered data structure that can store multiple items of different data types. Lists allow for various operations such as accessing, slicing, adding, and removing items, as well as utilizing built-in functions. They are ideal for situations where an ordered collection that can be modified is needed.
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)
0 views

Python List Full Notes

A Python list is a mutable, ordered data structure that can store multiple items of different data types. Lists allow for various operations such as accessing, slicing, adding, and removing items, as well as utilizing built-in functions. They are ideal for situations where an ordered collection that can be modified is needed.
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/ 4

PYTHON LISTS - FULL NOTES

1. What is a List?

- A List is a built-in data structure in Python that is used to store multiple items in a single variable.

- Lists are ordered, mutable (changeable), and allow duplicate values.

- Lists can store elements of different data types (integers, strings, floats, etc.).

2. Creating a List

Example:

numbers = [1, 2, 3, 4]

fruits = ['apple', 'banana', 'mango']

mixed = [10, 'hello', 3.5, True]

empty = [] # empty list

3. Accessing List Items

- Use index numbers (starting from 0).

- Negative indexing starts from the end (-1 for last item).

fruits[0] # 'apple'

fruits[-1] # 'mango'

4. List Slicing

- You can access a range of items using slicing.

fruits[0:2] # ['apple', 'banana']

fruits[:2] # ['apple', 'banana']

fruits[1:] # ['banana', 'mango']

5. Changing List Items


fruits[1] = 'orange'

print(fruits) # ['apple', 'orange', 'mango']

6. Looping Through a List

for fruit in fruits:

print(fruit)

7. Checking Existence

if 'apple' in fruits:

print('Yes')

8. List Length

len(fruits) # returns 3

9. Adding Items to a List

fruits.append('grape')

fruits.insert(1, 'kiwi')

10. Removing Items

fruits.remove('banana') # Removes first match

fruits.pop(2) # Removes by index

del fruits[0] # Deletes by index

fruits.clear() # Empties the list

11. List Methods

append(x) - Adds x to the end

insert(i, x) - Inserts x at index i


remove(x) - Removes first occurrence of x

pop([i]) - Removes item at index i

clear() - Empties the list

index(x) - Returns index of x

count(x) - Returns count of x

sort() - Sorts list (ascending by default)

reverse() - Reverses the list

copy() - Returns a shallow copy of the list

12. List Comprehension

squares = [x*x for x in range(5)] # [0, 1, 4, 9, 16]

13. Nested Lists

matrix = [[1, 2], [3, 4]]

print(matrix[0][1]) # 2

14. List vs Tuple

List is mutable (can be changed), Tuple is immutable (cannot be changed).

Lists use square brackets [], Tuples use parentheses ()

15. Copying a List

new_list = old_list.copy()

or: new_list = list(old_list)

16. Joining Lists

list1 + list2 # concatenates

list1.extend(list2)
17. Built-in Functions with Lists

len(list), max(list), min(list), sum(list), sorted(list)

18. Mutability of Lists

- Lists are mutable: their contents can be changed using indexing or methods like append(),

remove(), etc.

19. When to Use Lists

- Use lists when you need an ordered collection that can change (add/remove/update elements).

You might also like