Lists and Arrays
Subject: Data Structures (CSC 301)
A List and array are two common types of
linear data structures (others are
Stacks and
Queues). It is a way of storing a collection of elements in a specific order. The elements can be numbers, strings or objects and they’re usually accessed by their position (index). Example (real life) a shopping list may contain rice, beans, oil and salt.
In programming, we often need to store multiple values under one variable name. Instead of creating many separate variables, we use data structures like arrays and lists to organize data efficiently. e.g shopping_list = ["rice", "beans", "oil", "salt"]
Python does not have a built-in array data structure like C or Java, but it provides:• Lists (general-purpose, dynamic)
• Arrays (using the array module or numpy)
Array
An array is a collection of elements of the same data type, stored in a continuous memory location.
In Python, arrays are created using the array module.
Creating an Array in Python
import array
numbers = array.array('i', [10, 20, 30, 40])
Explanation:
- 'i' means integer type
- All elements must be integers
Array Operations1.
Access array: get an element by index
print(numbers[0]) # Output: 10
print(numbers[2]) # Output: 30
2.
Insertion: add an element
numbers.append(50)
print(numbers)
2.
Deletion: remove an element
numbers.remove(20)
print(numbers)
3.
Traversing an Array: go through all elements
for num in numbers:
print(num)
List
A list is also a collection of elements just like array; but unlike array, list can contain different data types and it is built-into the Python interpreter
Creating a list
students = ["Ada", "Bola", "Chinedu"]
List with different data types
person = ["John", 25, 5.8, True]
Accessing list elements
print(students[1]) # Output: Bola
List Operations1.
Adding Elements
students.append("Zainab")
print(students)
2.
Inserting at a Specific Position
students.insert(1, "Emeka")
3.
Removing Elements
students.remove("Bola")
4.
Updating Elements
students[0] = "Aisha"
5.
Traversing a List
for name in students:
print(name)
Differences between Arrays and Lists (Python perspective)
Data type- Arrays store elements of the same data type only (for example, all integers)
- Lists can store different data types such as integers, strings, and floats together.
Size- Arrays have a fixed size, meaning their structure is more rigid
- Lists are dynamic and can grow or shrink during program execution.
Speed- Arrays are generally faster because they store data in a more compact and efficient way.
- Lists are slightly slower due to their flexibility.
Flexibility- Arrays are less flexible since all elements must follow the same data type rule.
- Lists are highly flexible and support many built-in methods for easy manipulation.
Built-in support- Arrays are not fully built-in and require importing the array module.
- Lists are fully built-in and available by default in Python.
Memory usage- Arrays are more memory-efficient because they store data in a uniform format.
- Lists use more memory since they store references to objects.
Usage preference- Arrays are preferred when performance and memory efficiency are important.
- Lists are preferred for general-purpose programming and everyday tasks.
Similarities
• Both store multiple values
• Both are indexed
• Both support looping
• Both reduce code repetition
Assignment
Write a Python program that:
- Accepts five numbers from the user
- Stores them in a list
- Displays list (loop through to traverse)
- Sum the numbers and also display the sum
By:
Vision University
Login to comment or ask question on this topic
Previous Topic