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

Lists For Python

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)
2 views38 pages

Lists For Python

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

PYTHON LISTS

1. What is a List?
A List is a collection of items stored in a single variable.

Think of it like a shopping bag.

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

Here:

●​ apple → index 0
●​ banana → index 1
●​ mango → index 2
Lists are:

1.​ Ordered
2.​ Mutable (can change)
3.​ Allow duplicates
4.​ Can store different data types​

data = [10, "Hello", 3.14, True]

Teasy Explanation
List ante oka bag bro.

Oka bag lo chocolates, chips, coke anni pettinattu.

bag = ["chips", "coke", "chocolate"]

Bag lo unna items ni index tho access chestham.


2. Creating Lists
numbers = [1, 2, 3, 4]

names = ["John", "Bob", "Alice"]

mixed = [1, "Python", True]

empty = []

Output

[1, 2, 3, 4]

3. Accessing Elements
fruits = ["apple", "banana", "mango"]

print(fruits[0])

Output

apple

4. Negative Indexing
fruits = ["apple", "banana", "mango"]

print(fruits[-1])

Output

mango
Teasy
Positive index:

012

Negative index:

-3 -2 -1

Back side nundi count chestundi.

5. Slicing
Syntax:
list[start:end:step]

Example

nums = [1,2,3,4,5,6]

print(nums[1:4])

Output

[2,3,4]

6. List Length
nums = [1,2,3]

print(len(nums))
Output

7. Changing Values
fruits = ["apple","banana","mango"]

fruits[1] = "orange"

print(fruits)

Output

['apple', 'orange', 'mango']


LIST METHODS

8. append()
Adds one item at the end.

nums = [1,2,3]

[Link](4)

print(nums)

Output

[1,2,3,4]
Teasy
append = "last bench lo kurchopettu"

9. extend()
Adds multiple elements.

nums = [1,2]

[Link]([3,4,5])

print(nums)

Output

[1,2,3,4,5]
10. insert()
Insert at a specific position.

nums = [1,2,4]

[Link](2,3)

print(nums)

Output

[1,2,3,4]
11. remove()
Removes value.

nums = [1,2,3]

[Link](2)

print(nums)

Output

[1,3]

12. pop()
Removes by index.
nums = [1,2,3]

[Link]()

Output

List becomes:

[1,2]

13. clear()
Deletes everything.

nums = [1,2,3]
[Link]()

print(nums)

Output

[]

14. index()
Find a position.

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

print([Link]("banana"))
Output

15. count()
Count occurrences.

nums = [1,2,2,2,3]

print([Link](2))

Output

3
16. sort()
Ascending.

nums = [5,2,1,4]

[Link]()

print(nums)

Output

[1,2,4,5]

17. sort(reverse=True)
Descending.
[Link](reverse=True)

Output

[5,4,2,1]

18. reverse()
nums = [1,2,3]

[Link]()

print(nums)

Output

[3,2,1]
19. copy()
a = [1,2,3]

b = [Link]()

20. Difference Between Assignment and Copy


Wrong

a = [1,2,3]

b=a

Both point to the same list.


[Link](4)

print(b)

Output

[1,2,3,4]

Correct

b = [Link]()

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

Access

print(matrix[1][0])

Output : 3

ITERATING THROUGH LISTS


For Loop
nums = [10,20,30]

for num in nums:


print(num)
Output

10
20
30

enumerate()
fruits = ["apple","banana"]

for index, fruit in enumerate(fruits):


print(index, fruit)

Output

0 apple
1 banana
MEMBERSHIP OPERATORS
nums = [1,2,3]

print(2 in nums)

Output

True

LIST COMPREHENSION

Basic
squares = [x*x for x in range(5)]
print(squares)

Output

[0,1,4,9,16]

Even Numbers
evens = [x for x in range(20) if x%2==0]

Output

[0,2,4,6,8,10,12,14,16,18]

String Uppercase
words = ["hello","python"]
upper = [[Link]() for word in words]

Output

['HELLO', 'PYTHON']

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

flat = [num for row in matrix for num in row]

Output

[1,2,3,4]
ADVANCED SORTING

Sort by Length
words = ["cat","elephant","dog"]

[Link](key=len)

print(words)

Output

['cat','dog','elephant']

Sort by Last Character


[Link](key=lambda x: x[-1])
SHALLOW COPY VS DEEP COPY

Shallow Copy
import copy

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

b = [Link](a)

Inner lists still shared.

Deep Copy
b = [Link](a)

Everything is copied.

LIST UNPACKING
a,b,c = [10,20,30]

print(a)

Output

10

Star Unpacking
a,*b,c = [1,2,3,4,5]

Output

a=1
b = [2,3,4]
c=5

ZIP FUNCTION
names = ["Alice","Bob"]
marks = [90,80]

result = list(zip(names, marks))

print(result)
Output

[('Alice',90), ('Bob',80)]

MAP WITH LISTS


nums = [1,2,3]

result = list(map(lambda x:x*2, nums))

Output

[2,4,6]

FILTER WITH LISTS


nums = [1,2,3,4,5]

evens = list(filter(lambda x:x%2==0, nums))

Output

[2,4]

BIG-O ANALYSIS
Operation Complexity

Append O(1)

Access O(1)

Search O(n)

Insert Beginning O(n)


Remove O(n)

Sort O(n log n)

INTERVIEW LEVEL QUESTIONS


Reverse a List
nums = [1,2,3,4]

print(nums[::-1])

Output

[4,3,2,1]

Remove Duplicates
nums = [1,1,2,2,3]
result = list(set(nums))

Output

[1,2,3]

Find Second Largest


nums = [5,1,8,3]

[Link]()

print(nums[-2])

Output

EXPERT LEVEL
List as Stack
stack = []

[Link](10)

[Link](20)

[Link]()

LIFO

Last In First Out

List as Queue (Not Efficient)


queue = []

[Link](10)

[Link](0)
FIFO

First In First Out

Better:

from collections import deque

queue = deque()

HIGH LEVEL THINKING


A Python list is actually a dynamic array.

When capacity is full:

1.​ Python allocates bigger memory


2.​ Copies old elements
3.​ Inserts new element
That's why:

append()

is amortized O(1).

COMMON MISTAKES
Mistake 1
nums = [1,2,3]

print(nums[3])

Output

IndexError
Mistake 2
[Link](100)

Output

ValueError

Mistake 3
matrix = [[0]*3]*3

Bad!

Changing one row changes all rows.

Correct:
matrix = [[0 for _ in range(3)] for _ in range(3)]

15 PRACTICE PROGRAMS
1.​ Find largest element.
2.​ Find smallest element.
3.​ Find second largest.
4.​ Reverse a list.
5.​ Count even numbers.
6.​ Count odd numbers.
7.​ Remove duplicates.
8.​ Merge two lists.
9.​ Find common elements.
10.​Rotate list by k positions.
11.​Check if list is palindrome.
12.​Sort without using sort().
13.​Flatten nested list.
14.​Find frequency of each element.
15.​Separate positive and negative numbers.
10 TRICKY ASSIGNMENT QUESTIONS
1.​ Find all duplicate elements.
2.​ Find missing number from 1–100.
3.​ Move all zeros to end.
4.​ Find pair with given sum.
5.​ Find longest consecutive sequence.
6.​ Implement stack using list.
7.​ Implement queue using list.
8.​ Matrix transpose using nested lists.
9.​ Spiral traversal of matrix.
10.​Implement mini spreadsheet using lists.

Final Teasy Summary


List ante Python lo all-rounder bro
●​ Store chestadi
●​ Modify chestadi
●​ Sort chestadi
●​ Search chestadi
●​ Loop chestadi
●​ Data structures build chestadi

DSA lo almost anni:

●​ Stacks
●​ Queues
●​ Graphs
●​ Trees
●​ Dynamic Programming

anni lists meeda base ayi untayi.

You might also like