Array list in Python example

Stanley 155 Published: 06/15/2024

Array list in Python example

I'm happy to respond in English for once!

In Python, an array is not a built-in data structure like it is in some other languages, such as Java or C++. Instead, Python uses lists, which are mutable sequences of objects.

Here's an example of creating and manipulating a list in Python:

# Create a list with some initial elements

my_list = ["apple", "banana", "cherry"]

Add another element to the end of the list

my_list.append("date")

print(my_list) # Output: ['apple', 'banana', 'cherry', 'date']

Get the first element of the list

first_element = my_list[0]

print(first_element) # Output: apple

Get the last element of the list

last_element = my_list[-1]

print(last_element) # Output: date

Modify the second element of the list

my_list[1] = "orange"

print(my_list) # Output: ['apple', 'orange', 'cherry', 'date']

Remove an element from the middle of the list

del my_list[2]

print(my_list) # Output: ['apple', 'orange', 'date']

As you can see, lists in Python are very flexible and can be manipulated using various methods like append, insert, remove, and more.

Here's another example that shows how to create a list from scratch:

# Create an empty list

my_list = []

Add some elements to the list

my_list.append(1)

my_list.append("hello")

my_list.append(3.14)

print(my_list) # Output: [1, 'hello', 3.14]

You can also use the extend method to add multiple elements at once:

# Create an empty list

my_list = []

Add some elements to the list using extend

my_list.extend([2, "world", 4.56])

print(my_list) # Output: [1, 'hello', 3.14, 2, 'world', 4.56]

I hope these examples give you a sense of how lists work in Python!

Difference between list and array in Python with examples

A great question!

In Python, list and array are often used interchangeably, but they actually serve different purposes.

List (mutable)

A list is a built-in Python data structure that stores a collection of items, which can be any type of object. A list is characterized by its mutability, meaning it can be modified after creation. You can think of a list as an ordered collection of objects that can be dynamically added to or removed from.

Example:

my_list = [1, 2, 3]

print(my_list) # [1, 2, 3]

Add an item

my_list.append(4)

print(my_list) # [1, 2, 3, 4]

Remove an item

my_list.remove(2)

print(my_list) # [1, 3, 4]

Array (immutable)

An array is a lower-level data structure that stores a fixed-size collection of items of the same type. In Python, arrays are not built-in, but you can use libraries like NumPy to create arrays. Unlike lists, arrays are immutable, meaning their contents cannot be changed once they're created.

Example (using NumPy):

import numpy as np

my_array = np.array([1, 2, 3])

print(my_array) # [1 2 3]

You can't modify an array

try:

my_array[0] = 5

except TypeError:

print("Error: cannot modify an array")

Key differences

Here are the main differences between list and array:

Mutability: Lists are mutable, while arrays are immutable. Type: Lists can store objects of different types, whereas arrays must store objects of the same type (e.g., integers or floats). Size: Lists can grow or shrink dynamically, while arrays have a fixed size that's determined at creation time. Indexing: Both lists and arrays support indexing, but lists are generally more flexible when it comes to accessing elements.

When to use each

Here are some guidelines on when to use list vs. array:

Use list: When you need a dynamically growing or shrinking collection of objects. When you need to modify the contents of the collection. Use array (NumPy array): When you need an efficient, fixed-size collection of numbers or other homogeneous data. When you're working with large datasets and performance is crucial.

In summary, while both list and array can be used to store collections of objects in Python, they differ fundamentally in their mutability, type, size, and indexing behavior. By choosing the right data structure for your needs, you can write more efficient, effective, and maintainable code.