What is List in Python?
In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type, such as integers, strings, or even other lists. Lists are enclosed in square brackets [ ] and each item is separated by a comma.
Here is an example of a list containing integers:
my_list = [1, 2, 3, 4, 5]
And here is an example of a list containing strings:
fruits = ['apple', 'banana', 'orange']
You can also create a list by using the list() constructor.
numbers = list(range(1,6))
You can access an item in a list by its index, which is an integer value that starts from 0 for the first item. To access the second item in the list, you can use the following syntax:
You can also use negative indices to access items from the end of the list.
List have a lot of methods like append, insert, remove, pop, clear, index, count, sort, reverse etc. You can use these methods to manipulate the list.
fruits.append('mango') fruits.sort() fruits.remove('banana')
You can also loop through a list using the for loop.
for fruit in fruits: print(fruit)
What is Array in Python?
An array in Python is a data structure that stores a collection of items, similar to a list. However, unlike lists, arrays are homogeneous, meaning that all items in the array must be of the same data type (e.g., all integers, all float, etc.). Arrays are typically more efficient than lists for numerical computations and other operations that require fast element access.
You can create an array in Python using the array module. The array() function is used to create an array, and it takes two arguments: the data type of the elements and an iterable (such as a list) containing the elements.
from array import array my_array = array("i", [1, 2, 3, 4, 5]) # create an array of signed integers
Alternatively, you can use the numpy library, which provides a powerful array object and a large library of mathematical functions to work with arrays.
importnumpy as np my_array = np.array([1, 2, 3, 4, 5])
You can also specify the data type of the array elements when creating a numpy array.
my_array = np.array([1, 2, 3, 4, 5], dtype=np.float32)
You can access and manipulate the elements of an array in the same way as you would with a list, using indexing and slicing. You can also perform mathematical operations on arrays using numpy functions. For example, you can add two arrays together using the + operator or the add() function.
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = a + b
You can also perform matrix operations, like dot product, matrix multiplication, transpose and many more with numpy array.
In short, arrays are similar to lists, but they are more efficient for numerical computations and operations that require fast element access. Numpy arrays are more powerful. It has more functionality and is widely used in scientific computing and data analysis.
Converting List into Array
In Python, there are several ways to convert a list to an array:
- Using the array module: The array module provides the array() function, which can convert a list to an array by specifying the data type of the elements and the list.
from array import array my_list = [1, 2, 3, 4, 5] my_array = array("i", my_list)
- Using the numpy library: The numpy library provides the array() function, which can convert a list to an array with specifying the data type.
importnumpy as np my_list = [1, 2, 3, 4, 5] my_array = np.array(my_list)
- Using list comprehension: You can use list comprehension to convert a list to an array.
my_list = [1, 2, 3, 4, 5] my_array = [i for i in my_list]
- Using casting: You can cast a list to an array using the array() function.
my_list = [1, 2, 3, 4, 5] my_array = array(my_list)
- Using the astype() method of numpy array: You can use the astype() method of numpy array to convert a list to an array.
importnumpy as np my_list = [1, 2, 3, 4, 5] my_array = np.array(my_list).astype(np.float32)
All these methods will convert the list to an array, but using the numpy library is generally more powerful and efficient, especially when working with large arrays or doing scientific computations.