Lists
Lists
Learn how lists store multiple values in an ordered, flexible collection and why they are one of the most useful data structures in programming.
What is a List?
A list is a data structure used to store multiple items in a single variable or collection.
In simple words, a list is an ordered collection of values. Each value inside a list is called an element or item.
Example:
students = ["Aman", "Riya", "Sohan", "Meera"]
Here, students is a list that stores multiple student names.
Easy Real-Life Example
List as a Shopping List
Imagine you are going to a market. Instead of remembering each item separately, you write all items together in one shopping list.
shoppingList = ["Rice", "Milk", "Bread", "Eggs"]
A programming list works in the same way. It stores many related values together so that we can manage them easily.
Why are Lists Used?
Lists are used because programs often need to work with many values, not just one value.
Without a list, we may need many separate variables:
student1 = "Aman"
student2 = "Riya"
student3 = "Sohan"
student4 = "Meera"
This approach becomes difficult when there are many values. A list solves this problem:
students = ["Aman", "Riya", "Sohan", "Meera"]
Lists are Used For
- Storing student names.
- Storing marks or scores.
- Storing product prices.
- Storing shopping cart items.
- Storing task lists or to-do items.
- Storing search results.
- Storing menu items.
- Storing messages or notifications.
- Processing multiple records using loops.
- Building stacks, queues, trees, graphs, and other data structures.
Important Terms Related to Lists
| Term | Meaning | Example |
|---|---|---|
| List | A collection of multiple values. | [10, 20, 30] |
| Element / Item | Each value stored inside a list. | 10, 20 |
| Index | The position of an element in the list. | 0, 1, 2 |
| Length | Total number of elements in a list. | length([10, 20, 30]) = 3 |
| Mutable | Can be changed after creation. | Add, update, remove elements. |
| Ordered | Elements maintain their sequence. | First item remains first unless changed. |
General Syntax of a List
The exact syntax differs by language, but lists are usually written as values separated by commas inside brackets or list-like structures.
listName = [item1, item2, item3, item4]
Example:
numbers = [10, 20, 30, 40, 50]
names = ["Aman", "Riya", "Sohan"]
prices = [99.50, 150.00, 75.25]
Indexing in Lists
Each element in a list has a position called an index.
In many programming languages, indexing starts from 0.
fruits = ["Apple", "Banana", "Mango", "Orange"]
Index: 0 1 2 3
Value: Apple Banana Mango Orange
We can access list elements using indexes.
DISPLAY fruits[0] // Apple
DISPLAY fruits[2] // Mango
n elements, the last index is usually n - 1.
Length of a List
The length of a list means the total number of elements stored in it.
marks = [85, 90, 78, 88, 92]
length(marks) = 5
Length is useful when we want to loop through all elements of a list.
Accessing List Elements
Accessing means reading a specific element from the list using its index.
/*
This program accesses list elements.
*/
ENTRY POINT
DECLARE fruits AS LIST = ["Apple", "Banana", "Mango"]
DISPLAY fruits[0]
DISPLAY fruits[1]
DISPLAY fruits[2]
END ENTRY POINT
Expected Output
Apple
Banana
Mango
Updating List Elements
Since lists are usually mutable, we can update an existing element by assigning a new value to a specific index.
/*
This program updates an element in a list.
*/
ENTRY POINT
DECLARE scores AS LIST = [70, 80, 90]
SET scores[1] = 85
DISPLAY scores
END ENTRY POINT
Expected Output
[70, 85, 90]
The value at index 1 changed from 80 to 85.
Adding Elements to a List
We can add new elements to a list. Many languages support adding at the end or inserting at a specific position.
Add at the End
/*
This program adds an item at the end of a list.
*/
ENTRY POINT
DECLARE tasks AS LIST = ["Study", "Practice"]
ADD "Revise" TO END OF tasks
DISPLAY tasks
END ENTRY POINT
Expected Output
["Study", "Practice", "Revise"]
Insert at a Specific Position
/*
This program inserts an item at a specific index.
*/
ENTRY POINT
DECLARE numbers AS LIST = [10, 30, 40]
INSERT 20 AT INDEX 1 IN numbers
DISPLAY numbers
END ENTRY POINT
Expected Output
[10, 20, 30, 40]
Removing Elements from a List
Removing means deleting an element from a list. We may remove by value or by position depending on the programming language.
/*
This program removes an item from a list.
*/
ENTRY POINT
DECLARE colors AS LIST = ["Red", "Green", "Blue"]
REMOVE "Green" FROM colors
DISPLAY colors
END ENTRY POINT
Expected Output
["Red", "Blue"]
Traversing a List
List traversal means visiting each element of a list one by one.
Traversal is commonly done using loops.
/*
This program traverses a list.
*/
ENTRY POINT
DECLARE students AS LIST = ["Aman", "Riya", "Sohan"]
FOR index FROM 0 TO length(students) - 1
DISPLAY students[index]
END FOR
END ENTRY POINT
Expected Output
Aman
Riya
Sohan
Searching in a List
Searching means checking whether a specific value exists in a list.
/*
This program searches for a value in a list.
*/
ENTRY POINT
DECLARE names AS LIST = ["Aman", "Riya", "Sohan", "Meera"]
DECLARE target AS TEXT = "Sohan"
DECLARE isFound AS BOOLEAN = false
FOR index FROM 0 TO length(names) - 1
IF names[index] == target THEN
SET isFound = true
BREAK
END IF
END FOR
IF isFound == true THEN
DISPLAY target + " found"
ELSE
DISPLAY target + " not found"
END IF
END ENTRY POINT
Expected Output
Sohan found
Sorting a List
Sorting means arranging list elements in a specific order, such as ascending or descending order.
/*
This program sorts a list in ascending order.
*/
ENTRY POINT
DECLARE numbers AS LIST = [40, 10, 30, 20]
SORT numbers IN ASCENDING ORDER
DISPLAY numbers
END ENTRY POINT
Expected Output
[10, 20, 30, 40]
Slicing a List
Slicing means extracting a smaller part of a list.
numbers = [10, 20, 30, 40, 50]
slice(numbers, 1, 4) = [20, 30, 40]
Slicing is useful when we need only a selected range of elements.
Nested Lists
A list can contain another list inside it. This is called a nested list.
marks = [
[80, 75, 90],
[85, 88, 92],
[70, 65, 78]
]
Nested lists are useful for representing tables, matrices, grids, and multi-level data.
marks[1][2] → 92
List vs Array
Lists and arrays are both used to store multiple values, but they may behave differently depending on the programming language.
| Feature | Array | List |
|---|---|---|
| Basic Meaning | Stores multiple elements, often in fixed-size structure. | Stores multiple elements in a flexible collection. |
| Size | Often fixed in many languages. | Often dynamic and resizable. |
| Access | Fast index-based access. | May support index-based access depending on implementation. |
| Insertion / Deletion | May require shifting elements. | Can be easier depending on list type. |
| Best For | Fixed-size collections and frequent direct access. | Flexible collections where items may be added or removed. |
Array List vs Linked List
Many languages provide different list implementations. Two common ideas are array-based lists and linked lists.
| Feature | Array-based List | Linked List |
|---|---|---|
| Storage Style | Uses array-like storage. | Uses nodes connected by links. |
| Access by Index | Usually fast. | Usually sequential. |
| Insertion / Deletion | May require shifting elements. | Can be efficient when links are adjusted. |
| Memory | Stores elements in array-like structure. | Needs extra memory for links or references. |
| Beginner View | Easy to understand after arrays. | Useful for learning dynamic memory and links. |
Common List Operations
| Operation | Meaning | Example Idea |
|---|---|---|
| Create | Make a new list. | numbers = [1, 2, 3] |
| Access | Read element by index. | numbers[0] |
| Update | Change element value. | numbers[1] = 25 |
| Add | Add new element. | Add item at end. |
| Insert | Add element at a specific position. | Insert at index 2. |
| Remove | Delete element. | Remove by value or index. |
| Traversal | Visit every element. | Use loop. |
| Search | Find whether value exists. | Find student name. |
| Sort | Arrange elements in order. | Ascending marks. |
| Slice | Extract part of a list. | First three items. |
Example: Calculate Total Marks Using a List
/*
This program calculates total marks using a list.
*/
ENTRY POINT
DECLARE marks AS LIST = [85, 90, 78, 88, 92]
DECLARE total AS INTEGER = 0
FOR index FROM 0 TO length(marks) - 1
SET total = total + marks[index]
END FOR
DISPLAY "Total Marks: " + total
END ENTRY POINT
Expected Output
Total Marks: 433
Example: Find Highest Value in a List
/*
This program finds the highest value in a list.
*/
ENTRY POINT
DECLARE scores AS LIST = [45, 78, 92, 66, 81]
DECLARE highest AS INTEGER = scores[0]
FOR index FROM 1 TO length(scores) - 1
IF scores[index] > highest THEN
SET highest = scores[index]
END IF
END FOR
DISPLAY "Highest Score: " + highest
END ENTRY POINT
Expected Output
Highest Score: 92
Advantages of Lists
Benefits
- Lists store multiple values under one name.
- Lists maintain the order of elements.
- Lists can usually grow or shrink dynamically.
- Lists make it easy to add and remove elements.
- Lists work well with loops.
- Lists reduce the need for many separate variables.
- Lists are useful for real-world collections such as carts, tasks, scores, and names.
- Lists help students understand more advanced data structures.
Limitations of Lists
Limitations
- Some list operations may become slower for very large collections.
- Inserting or deleting from the middle may require extra work depending on implementation.
- Accessing invalid indexes can cause errors.
- Lists can use more memory than simple variables.
- Nested lists can become difficult to read if not organized properly.
Common Beginner Mistakes
Mistakes
- Thinking the first index is
1instead of0. - Trying to access an index outside the list range.
- Confusing list length with last index.
- Forgetting that the last index is usually
length - 1. - Removing items while looping without careful logic.
- Using many variables instead of one list.
- Not using loops for list traversal.
- Confusing arrays, lists, and linked lists.
Better Habits
- Remember that indexing usually starts from
0. - Use
length - 1for the last valid index. - Check list length before accessing elements.
- Use meaningful plural names such as
students,marks, andprices. - Use loops to process list elements.
- Test with empty, one-element, and many-element lists.
- Use comments when list logic is complex.
- Choose the right list type based on access, insertion, and deletion needs.
Best Practices for Lists
Recommended Practices
- Use lists when you need to store multiple related values.
- Use meaningful list names in plural form.
- Use loops for traversal instead of repeated statements.
- Check valid indexes before accessing items.
- Use add, insert, update, and remove operations carefully.
- Keep nested lists readable and well-formatted.
- Use sorting when ordered data is required.
- Use searching when you need to check whether an element exists.
- Do not overuse lists when a single variable is enough.
- Practice lists with real-world examples such as shopping carts, marksheets, and task managers.
Prerequisites Before Learning Lists
Students should already understand:
Required Knowledge
- Variables and constants.
- Data types.
- Input and output.
- Operators.
- Conditions.
- Loops and iteration.
- Arrays and indexing basics.
- Array traversal.
- Searching and sorting basics.
Trace Table Example
Let us trace the total calculation of a list:
numbers = [10, 20, 30]
total = 0
FOR index FROM 0 TO length(numbers) - 1
SET total = total + numbers[index]
END FOR
| Iteration | index | numbers[index] | total before | total after |
|---|---|---|---|---|
| 1 | 0 |
10 |
0 |
10 |
| 2 | 1 |
20 |
10 |
30 |
| 3 | 2 |
30 |
30 |
60 |
Final value of total is 60.
Practice Activity: Work with a List
Study the following list:
colors = ["Red", "Green", "Blue", "Yellow"]
Questions
1. What is the value at index 0?
2. What is the value at index 2?
3. What is the length of the list?
4. What is the last index?
5. What will the list become after adding "Black" at the end?
Sample Answers
1. Red
2. Blue
3. 4
4. 3
5. ["Red", "Green", "Blue", "Yellow", "Black"]
Mini Quiz
What is a list?
A list is an ordered collection of multiple values stored under one name.
What is an element in a list?
An element is a single value stored inside a list.
What is list indexing?
List indexing means accessing elements using their position numbers.
Why are lists useful?
Lists are useful because they store multiple related values together and allow easy processing using loops.
What does mutable mean?
Mutable means the list can be changed after creation by adding, updating, or removing elements.
Interview Questions on Lists
Define list in programming.
A list is a data structure that stores multiple elements in an ordered sequence.
How is a list different from a normal variable?
A normal variable stores one value, while a list can store multiple values.
What are common operations performed on lists?
Common operations include create, access, update, add, insert, remove, traverse, search, sort, and slice.
What is the difference between an array-based list and a linked list?
An array-based list stores elements using array-like storage, while a linked list stores elements as connected nodes.
Why should beginners learn lists?
Beginners should learn lists because lists are used in loops, searching, sorting, data processing, and many advanced data structures.
Quick Summary
| Concept | Meaning |
|---|---|
| List | Stores multiple values in an ordered collection. |
| Element | A value stored inside a list. |
| Index | Position number of an element. |
| Length | Total number of elements. |
| Mutable | Can be changed after creation. |
| Traversal | Visiting each element one by one. |
| Common Uses | Students, marks, products, tasks, carts, menus, and records. |
Final Takeaway
Lists are one of the most important data structures in programming. They allow us to store multiple values together, access values by index, update elements, add new items, remove old items, traverse data with loops, search values, and sort collections. In the Programming Mastery Course, students should understand lists as flexible collections that prepare them for advanced topics such as stacks, queues, linked lists, trees, graphs, and real-world data processing.