-
Hajipur, Bihar, 844101
Lists and arrays are the most commonly used data structures in Python. They help you store multiple values, organize information and perform operations such as adding, removing, sorting and searching. If you are building a billing app, a student record system, an inventory tool or a data-processing script, you will work with lists or arrays all the time.
This chapter explains how lists work in Python, how arrays differ from lists, how to use both structures and when to choose one over the other. The goal is to make you comfortable with storing and manipulating collections of data in real programs.
A list is an ordered, changeable collection of items. Each item is stored at a specific index, starting from zero. Lists can contain numbers, strings, booleans or even other lists.
fruits = ["apple", "banana", "mango"]
Lists are flexible because you can add, delete or update values anytime. This makes them ideal for handling dynamic information such as user responses, product lists or schedules.
You access items using the index.
print(fruits[0]) # apple
print(fruits[2]) # mango
Negative indexing lets you start from the end.
print(fruits[-1]) # mango
This helps when you want the last added element or want to navigate backward.
Lists allow updates easily.
fruits[1] = "grapes"
print(fruits)
This comes in handy when user data changes or product details get updated.
Adds an item at the end.
fruits.append("orange")
Adds an item at a specific position.
fruits.insert(1, "guava")
Both methods help when building programs that accept dynamic input from forms or APIs.
Removes by value.
fruits.remove("apple")
Removes by index and returns the removed value.
fruits.pop(1)
Deletes an element or the whole list.
del fruits[0]
These are useful when processing user requests, deleting records or updating lists.
Slicing helps you retrieve a part of the list.
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
You can also skip values:
print(nums[::2]) # [10, 30, 50]
This is helpful for pagination, selecting intervals or processing chunks.
The for loop is commonly used to access items one by one.
for item in fruits:
print(item)
This approach is ideal when sending notifications, printing reports or validating entries.
You can find how many items a list contains.
print(len(fruits))
This is useful when building dashboards or validating minimum requirements.
nums = [5, 2, 9, 1]
nums.sort()
print(nums)
Does not change the original list.
Sorting helps in ranking, filtering, analyzing data or arranging lists alphabetically.
You can check if a value exists:
print("apple" in fruits)
Or find the index:
print(fruits.index("banana"))
This is helpful in search bars, validation or autocomplete features.
Lists should not be copied using = because both variables will point to the same list.
Use:
new_list = fruits.copy()
This avoids unwanted changes in real projects where multiple functions handle the same data.
Lists can contain other lists.
matrix = [
[1, 2],
[3, 4]
]
Nested lists help represent tables, grids or two-dimensional data.
Python does not have traditional arrays like C or Java by default, but it provides two ways to work with arrays:
The built-in list (used as a dynamic array)
The array module (for arrays containing only one data type)
Most Python programs use lists because they are more flexible.
You must import the module:
from array import array
nums = array('i', [10, 20, 30])
Here 'i' means the array stores integers only. This gives better performance in situations like scientific calculations or sensor data readings.
nums.append(40)
nums.remove(20)
The methods work similarly to lists but with strict type rules.
print(nums[1])
Just like lists, arrays use indexing.
Data types vary
Frequent insertions and deletions
You need flexibility
You are building user-facing apps or web systems
All values share the same type
You need better performance
You work on numerical or scientific tasks
Store daily tasks as a list that changes often.
Use an array when all marks are integers and you need faster operations.
Lists hold product names, prices and quantities.
Arrays store continuous numeric data for long durations.
nums = [10, 20, 30]
print(sum(nums))
max(nums)
users = ["Aarti", "Nisha", "Meera"]
users.insert(1, "Riya")
users.pop()
evens = [n for n in nums if n % 2 == 0]
from array import array
vals = array('i', [1, 2, 3])
"mango" in fruits
nums.sort()
nums[1:4]
Lists and arrays help you collect and manage multiple values smoothly. You learned how to create lists, update them, slice them, sort them and search for elements. You also explored arrays using the array module and understood when arrays offer better performance. These structures are the foundation of most Python programs, from billing tools to dashboards to data analysis scripts. By mastering them, you get ready for more advanced structures like stacks, queues, linked lists and trees.
Q1. Write a Python program to create a list of your favorite fruits and print it.
Q2. Write a Python program to replace the 2nd element in a list with a new value.
Q3. Write a Python program to append a new number to a list.
Q4. Write a Python program to use pop() to remove the last element from a list.
Q5. Write a Python program to check if a value exists in a list using the in keyword.
Q6. Write a Python program to create an integer array using array.array.
Q7. Write a Python program to use a loop to print all values in an array.
Q8. Write a Python program to insert a value at index 1 in a list.
Q9. Write a Python program to use remove() to delete a value from a list.
Q10. Write a Python program to count how many times an element appears in a list using count().