3.10-3.11 Lists and Searches
Basic Operations
#Defining the List
aList= [1,4,2,6,7,3,]
# Accessing Elements in a list
print(aList[1])
#Storing Element at an Index to a variable
Element = aList[3]
print(Element)
#Setting an Element at an Index to a variable
element = 4
aList[5]= element
print(aList[5])
#Insert a value at a certain index
aList.insert(3, 10)
print(aList[3])
#Adding another value to the list (append)
aList.append(5)
print(aList)
#Removing a value from the List at a specific Index
aList.remove(2)
print(aList)
#Accessing the Length of a list
print(len(aList))
4
6
4
10
[1, 4, 2, 10, 6, 7, 4, 5]
[1, 4, 10, 6, 7, 4, 5]
7
PopCorn hack #1
- Create and define your own list and insert a value at a certain index
#Enter code here
alist = [1,2,3,4,5,6,7,8,9]
alist.append(10)
print(alist)
[2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
Finding the Minimum Value in a List
nums = [30, 45, 95, 56, 73, 98, 25]
min_value = nums[4] # Start with the first element as the minimum
for score in nums:
if score < min_value:
min_value = score
print(min_value) # Display the minimum value
25
PopCorn hack #2
- Using the code above try to find the maximum value in the list
#Enter Code here
nums = [30, 45, 95, 56, 73, 98, 25]
max_value = nums[4]
for score in nums:
if score > max_value:
max_value = score
print(max_value)
98
Sum of Even Numbers in a list solution
#Defining numbers in list
nums = [7, 5, 10, 6, 9, 4, 3, 12]
sum_even = sum([score for score in nums if score % 2 == 0]) #Using for loop to work through each number in the list and using mod to interpret if any numbers have a remainder of 0
print(sum_even)
32
Determining Outputs for code Segments with Length function
#Defining the list
words = ['old', 'computer', 'science', 'far', 'potato']
new_words = []
for word in words:
if len(word) != 3: #if the length of the word is not equal to three
new_words.append(word) #adds it to a new list if the length is not equal to three
print(new_words)
['computer', 'science', 'potato']
If Else Statements with Lists
Performing a Binary Search
- index: organizing the data by assigning a reference value to each element
- Put the number is order either ascending or descending
- Search starts with middle number first which is found by adding the highest and lowest index number and dividing it by 2
- This divides the range by 2
- Repeat this process by shrinking the range each time till the desired target is found
- Every time the process is repeated and leads to a target it is considered a comparison
Performing a Sequential Search
- Each element in a list is examined in the order of the first element till the desired target
- Order doesn’t matter
Homework Hack 1
- What will be the middle index number in a Binary Search given the set of number: 3, 6, 12, 14, 50, 53
- Which of the following lists can a Binary Search be used to find the desired target
- This is a multiple choice question
fruits= ['Apple', 'Carrot', 'Mango', 'Peach']
A= [1, 3, 5, 8, 9, ]
B= [6, -1, 5, 12, 8]
#Type your choices below
print("answer for number 1 is A")
print("answer for number 2 is A")
answer for number 1 is A
answer for number 2 is A
Homework Hack 2
- Given a provided list perform two of the basic operations
yourlist = [8,10,35,39,49,52]
#type out your answers below and run the code and include output in screenshot
#Insert a value at a certain index
yourlist.insert(2, 20)
print("After inserting 20 at index 2:", yourlist)
#Removing a value from the list
yourlist.remove(39)
print("After removing 39 from the list:", yourlist)
#Sort the list in ascending order
yourlist.sort()
print("Sorted list in ascending order:", yourlist)
#Find the sum of all even numbers in the list
sum_even = sum([num for num in yourlist if num % 2 == 0])
print("Sum of even numbers:", sum_even)
After inserting 20 at index 2: [8, 10, 20, 35, 39, 49, 52]
After removing 39 from the list: [8, 10, 20, 35, 49, 52]
Sorted list in ascending order: [8, 10, 20, 35, 49, 52]
Sum of even numbers: 90
Reflection:
Lists:
-
Lists are basic data structures for storing collections of items.
-
Arrays offer fast access but fixed sizes, while linked lists are dynamic and great for insertions and deletions.
-
Dynamic arrays like Python’s list resize as needed.
-
Common operations include adding, removing, searching, and iterating through items.
-
Skills in list manipulation, like sorting and filtering, are vital.
Searching Algorithms:
-
Linear search scans items one by one, suitable for small lists.
-
Binary search is efficient for finding items in sorted lists, using a divide-and-conquer approach.
-
Understanding time complexity is essential (linear: O(n), binary: O(log n)).
-
You can implement search algorithms using recursion or iteration.
-
Searching is used in real-world scenarios like databases and text documents.
-
Complex search problems include graph and string searching.
-
Consider performance, memory, and trade-offs when choosing a search algorithm.