0% found this document useful (0 votes)
2 views8 pages

Python List Notes

This document provides an overview of Python lists and dictionaries, detailing how to create, access, modify, and manage these data structures. It highlights key operations such as adding, removing, and checking items in lists, as well as the differences between lists and tuples. Additionally, it explains the structure and limitations of dictionaries, including key-value pairs and the uniqueness of keys.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Python List Notes

This document provides an overview of Python lists and dictionaries, detailing how to create, access, modify, and manage these data structures. It highlights key operations such as adding, removing, and checking items in lists, as well as the differences between lists and tuples. Additionally, it explains the structure and limitations of dictionaries, including key-value pairs and the uniqueness of keys.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python List Notes

1) Create list: Creates a list


with [Link] =
["apple", "banana",
"mango"]
2) Index: Gets item by
position.
fruits[0] =
apple fruits[1]
= banana
3) Negative index: Gets last
[Link][-1] = mango
4) Slicing: Gets part of
[Link][0:2] = ['apple',
'banana']
5) Change value: Changes
[Link][1] = "orange"
6) Add item: Adds at
[Link]("grapes
")
7) Insert item: Adds at
specific position.
[Link](1, "kiwi")
8) Remove item: Removes
item by
[Link]("apple
")
9) Delete by index: Deletes
item by [Link] fruits[0]
10) Pop item: Removes last
[Link]()
11) Length: Counts
[Link](fruits)
12) Sort: Arranges in A-Z
[Link]()
13) Reverse: Reverses
[Link]()
14) Loop list: Prints each
[Link] x in fruits:
print(x)
15) Check item: Checks if
item exists.
16) "apple" in fruits

True / False

17) Mostly yes, tuples and lists


are similar because both
store multiple items.

18) Example:

19) List:

20) fruits= ["apple", "banana",


"mango"]

21) Tuple:
22) fruits= ("apple", "banana",
"mango")

23) Both support:


✅ Index
✅ Negative index
✅ Slicing
✅ Loop
✅ Check item
✅ Length

24) Main difference:

25) List → can change

26) fruits = ["apple", "banana"]

27) fruits[0] = "orange"

28) ✅ Allowed
29)

30) Tuple → cannot change

31) fruits = ("apple", "banana")

32) fruits[0] = "orange"

33) ❌ Error

34) Also list has:

35) append()

36) insert()

37) remove()

38) pop()

39) sort()
40) Tuple
does not have these
because tuple cannot change.

41) Simple:

42) List = editable ✏️

43) Tuple = fixed / cannot edit 🔒

In Python, dictionary stores


data in key : value pairs.

It uses curly brackets { }.

Example:

student = {

"name": "Ali",

"age": 25,

"city": "Dubai"

Here:

 name → key

 Ali → value

Simple meaning:
Dictionary = data stored as
key and value pairs 🔑

Example:

student["name"]

Output:

Ali
Main things about
Dictionary

1) Create dictionary

student = {"name":"Ali",
"age":25}

2) Access value

student["name"]

➡️Gets value of key "name"

3) Change value

student["age"] = 26

➡️Changes value

4) Add item

student["city"] = "Dubai"

➡️Adds new key-value pair

5) Remove item

[Link]("age")

➡️Removes key "age"

6) Length

len(student)
➡️Counts items

7) Keys

[Link]()

➡️Shows all keys

8) Values

[Link]()

➡️Shows all values

9) Loop

for x in student:

print(x)

➡️Prints keys

Example:

name

age

Simple meaning:
Dictionary = store data with
names (keys) instead of
index numbers.

Dictionary can do many


things, but compared to
list/tuple, here’s what it can’t
do:
1) No index access

❌ Not like list:

student[0]

Error

Because dictionary uses


keys, not index numbers.

Correct:

student["name"]

2) No slicing

student[0:2]

Error

Dictionary does not support


slicing.

3) Duplicate keys not


allowed

student = {

"name": "Ali",

"name": "John"

Output:

{'name': 'John'}
➡️Old value gets replaced.

4) Keys must be unique /


immutable

Good keys:
✅ String
✅ Number
✅ Tuple

Bad key:

student = {

[1,2]: "value"

❌ Error because list cannot be


key.

Simple:

 Dictionary can’t use


index

 Dictionary can’t slice

 Dictionary can’t have


duplicate keys

 Dictionary can’t use


mutable keys like list

You might also like