Python Lists and Arrays


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.

What Are Lists in Python?

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.

Accessing List Items

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.

Changing List Values

Lists allow updates easily.

fruits[1] = "grapes"
print(fruits)

This comes in handy when user data changes or product details get updated.

Adding Items to a List

append()

Adds an item at the end.

fruits.append("orange")

insert()

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.

Removing Items from a List

remove()

Removes by value.

fruits.remove("apple")

pop()

Removes by index and returns the removed value.

fruits.pop(1)

del

Deletes an element or the whole list.

del fruits[0]

These are useful when processing user requests, deleting records or updating lists.

List Slicing

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.

Looping Through Lists

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.

Checking Length

You can find how many items a list contains.

print(len(fruits))

This is useful when building dashboards or validating minimum requirements.

Sorting Lists

sort()

nums = [5, 2, 9, 1]
nums.sort()
print(nums)

sorted()

Does not change the original list.

Sorting helps in ranking, filtering, analyzing data or arranging lists alphabetically.

Searching in a List

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.

Copying Lists

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.

Nested Lists

Lists can contain other lists.

matrix = [
    [1, 2],
    [3, 4]
]

Nested lists help represent tables, grids or two-dimensional data.

What Are Arrays in Python?

Python does not have traditional arrays like C or Java by default, but it provides two ways to work with arrays:

  1. The built-in list (used as a dynamic array)

  2. The array module (for arrays containing only one data type)

Most Python programs use lists because they are more flexible.

Arrays Using the array Module

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.

Adding and Removing Array Items

nums.append(40)
nums.remove(20)

The methods work similarly to lists but with strict type rules.

Accessing Array Items

print(nums[1])

Just like lists, arrays use indexing.

When to Use Lists vs Arrays

Use Lists When:

  • Data types vary

  • Frequent insertions and deletions

  • You need flexibility

  • You are building user-facing apps or web systems

Use Arrays When:

  • All values share the same type

  • You need better performance

  • You work on numerical or scientific tasks

Real-Life Use Cases

To-Do App

Store daily tasks as a list that changes often.

Student Marks

Use an array when all marks are integers and you need faster operations.

Billing System

Lists hold product names, prices and quantities.

Sensor Readings

Arrays store continuous numeric data for long durations.

Practical Examples

Example 1: Add all values

nums = [10, 20, 30]
print(sum(nums))

Example 2: Find maximum

max(nums)

Example 3: List of user names

users = ["Aarti", "Nisha", "Meera"]

Example 4: Insert new item

users.insert(1, "Riya")

Example 5: Remove last item

users.pop()

Example 6: Filter even numbers

evens = [n for n in nums if n % 2 == 0]

Example 7: Array of integers

from array import array
vals = array('i', [1, 2, 3])

Example 8: Searching in a list

"mango" in fruits

Example 9: Sorting numeric list

nums.sort()

Example 10: Slicing list

nums[1:4]

Summary of the Tutorial

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.


Practice Questions

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().


Try a Short Quiz.

Python Lists and Arrays Quiz

Wrapped up the topic? This quick quiz will help you practice and remember it better.


Go Back Top