1 Dimensional Arrays
Introduction to One-Dimensional Arrays
What is an Array?
One-Dimensional Arrays
["cat","dog","mouse","bear"]
Declaring and Initializing One-Dimensional Arrays
my_list = [10, 20, 30, 40, 50] # Creating a list of integers
What is an array?
How do you declare a one-dimensional array in most programming languages?
How do you find the length of an array in Python?
Looping through arrays
To loop through a Python list (also known as traversing a list/array), you can use various types of loops, such as the for loop or the while loop. Here's how to do it using the for loop:
# Using a for loop to iterate through a list (array)
my_list = [10, 20, 30, 40, 50]
# Method 1: Using range and len
for i in range(len(my_list)):
print(my_list[i])
# Method 2: Using direct iteration
for item in my_list:
print(item)
Searching Arrays
You can search for the index of an item in a list:
# Searching for an element in a Python list and printing its index
# Sample list
my_list = [10, 20, 30, 40, 50]
# Search value
search_value = 30
# Using the index() method
if search_value in my_list:
index = my_list.index(search_value)
print(f"{search_value} found at index {index}.")
else:
print(f"{search_value} not found in the list.")
Adding Items to a List
You can add and remove items from a Python list using various built-in methods. Here's how you can do it:
Append:
Adds an item to the end of the list.
my_list = [10, 20, 30]
my_list.append(40)
print(my_list) # Output: [10, 20, 30, 40]
Insert:
Inserts an item at a specific index.
my_list = [10, 20, 30]
my_list.insert(1, 15)
print(my_list) # Output: [10, 15, 20, 30]
Extend:
Appends elements from another iterable to the end of the list.
my_list = [10, 20, 30]
my_list.extend([40, 50])
print(my_list) # Output: [10, 20, 30, 40, 50]
Removing Items from a List
Remove: Removes the first occurrence of a specific value.
my_list = [10, 20, 30, 20, 40]
my_list.remove(20)
print(my_list) # Output: [10, 30, 20, 40]
Pop: Removes and returns the item at a specific index.
my_list = [10, 20, 30, 40]
removed_item = my_list.pop(1)
print(removed_item) # Output: 20
print(my_list) # Output: [10, 30, 40]
Review: Fill in the Blanks
Initialization of a 1 Dimensional Array involves declaring the data type, specifying the array name, and indicating the array size. Elements within the array can be initialized individually or using a loop. Once initialized, elements can be ed by referencing their corresponding index. This allows for easy retrieval and manipulation of data.
of a 1 Dimensional Array refers to the process of accessing each element within the array in a sequential manner. This is commonly done using a loop that iterates over the array, visiting each index and performing necessary operations on the elements.
functionality can be implemented on a 1 Dimensional Array to locate a specific element. By systematically comparing each element with the desired value, it is possible to determine if the element exists within the array. involves placing a new element at a specific position in the array, pushing other elements to accommodate the new addition. , on the other hand, removes an element from the array, readjusting the remaining elements accordingly.
An element can be considered as a single unit of data within an array, representing a unique value. To an array means to visit each element one by one. Updating an element involves modifying its value according to the requirements of the program.
Lastly, ing a 1 Dimensional Array entails rearranging the elements in a particular order, such as ascending or descending. This can be achieved using sorting algorithms that compare and swap elements until the desired order is achieved.
Complete! Ready to test your knowledge?
One dimensional arrays
- Introduction to One-Dimensional Arrays
- Looping through arrays
- Searching Arrays
- Adding Items to a List
- Removing Items from a List