Python list это массив

Array vs. List in Python – What’s the Difference?

Both lists and arrays are used to store data in Python. Moreover, both data structures allow indexing, slicing, and iterating. So what’s the difference between an array and a list in Python? In this article, we’ll explain in detail when to use a Python array vs. a list.

Python has lots of different data structures with different features and functions. Its built-in data structures include lists, tuples, sets, and dictionaries. However, this is not an exhaustive list of the data structures available in Python. Some additional data structures can be imported from different modules or packages.

An array data structure belongs to the «must-import» category. To use an array in Python, you’ll need to import this data structure from the NumPy package or the array module.

And that’s the first difference between lists and arrays. Before diving deeper into the differences between these two data structures, let’s review the features and functions of lists and arrays.

What Is a List in Python?

A list is a data structure that’s built into Python and holds a collection of items. Lists have a number of important characteristics:

  • List items are enclosed in square brackets, like this [item1, item2, item3].
  • Lists are ordered – i.e. the items in the list appear in a specific order. This enables us to use an index to access to any item.
  • Lists are mutable, which means you can add or remove items after a list’s creation.
  • List elements do not need to be unique. Item duplication is possible, as each element has its own distinct place and can be accessed separately through the index.
  • Elements can be of different data types: you can combine strings, integers, and objects in the same list.

Lists are very easily created in Python:

list = [3, 6, 9, 12] print(list) print(type(list))

Python lists are used just about everywhere, as they are a great tool for saving a sequence of items and iterating over it.

What Is an Array in Python?

An array is also a data structure that stores a collection of items. Like lists, arrays are ordered, mutable, enclosed in square brackets, and able to store non-unique items.

Читайте также:  Img Tag

But when it comes to the array’s ability to store different data types, the answer is not as straightforward. It depends on the kind of array used.

To use arrays in Python, you need to import either an array module or a NumPy package.

The Python array module requires all array elements to be of the same type. Moreover, to create an array, you’ll need to specify a value type. In the code below, the «i» signifies that all elements in array_1 are integers:

array_1 = arr.array("i", [3, 6, 9, 12]) print(array_1) print(type(array_1))

On the other hand, NumPy arrays support different data types. To create a NumPy array, you only need to specify the items (enclosed in square brackets, of course):

array_2 = np.array(["numbers", 3, 6, 9, 12]) print (array_2) print(type(array_2))

As you can see, array_2 contains one item of the string type (i.e., «numbers») and four integers.

So What’s the Difference?

Now that we know their definitions and features, we can talk about the differences between lists and arrays in Python:

  • Arrays need to be declared. Lists don’t, since they are built into Python. In the examples above, you saw that lists are created by simply enclosing a sequence of elements into square brackets. Creating an array, on the other hand, requires a specific function from either the array module (i.e., array.array()) or NumPy package (i.e., numpy.array()). Because of this, lists are used more often than arrays.
  • Arrays can store data very compactly and are more efficient for storing large amounts of data.
  • Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code. If you try the same with a list, you’ll get an error.
array = np.array([3, 6, 9, 12]) division = array/3 print(division) print (type(division))
list = [3, 6, 9, 12] division = list/3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 1 list = [3, 6, 9, 12] ----> 2 division = list/3 TypeError: unsupported operand type(s) for /: 'list' and 'int'

Of course, it’s possible to do a mathematical operation with a list, but it’s much less efficient:

Code Editor

So, when should you use a list and when should you use an array?

  • If you need to store a relatively short sequence of items and you don’t plan to do any mathematical operations with it, a list is the preferred choice. This data structure will allow you to store an ordered, mutable, and indexed sequence of items without importing any additional modules or packages.
  • If you have a very long sequence of items, consider using an array. This structure offers more efficient data storage.
  • If you plan to do any numerical operations with your combination of items, use an array. Data analytics and data science rely heavily on (mostly NumPy) arrays.
Читайте также:  Php fpm nginx example

Time to Practice Python Arrays and Lists!

Great! Now you know the difference between an array and a list in Python. You also know which to choose for a sequence of items. Now it’s time to practice!

If you want to advance your understanding of data structures and practice 100+ interactive exercises, check out the LearnPython.com course Python Data Structures in Practice. It will help you feel like a pro when dealing with lists, nested lists, tuples, sets, and dictionaries.

Источник

Чем отличается массив от списка Python – таблица сравнения

Массивы и списки Python являются важной структурой данных Python. И список, и массив используются для хранения данных в Python. Эти структуры данных позволяют нам индексировать, нарезать и повторять. Но они отличаются друг от друга. В этом руководстве мы узнаем существенные различия между списком и массивом Python.

Вступление

Как мы знаем, Python имеет обширные структуры данных, такие как списки, кортежи, наборы и словари, которые предоставляют множество возможностей и функций. Списки являются наиболее эффективной и простой в использовании структурой данных в Python.

С другой стороны, Python не предоставляет встроенной поддержки массива. Нам нужно импортировать модуль массива, или использовать модуль массива из пакета NumPy в программе Python. И это основное различие между массивом и списком. Прежде чем углубиться в эту тему, давайте кратко познакомимся с обеими структурами данных.

Список

Список в Python – это встроенная линейная структура данных Python. Он используется для последовательного хранения данных. Мы можем выполнить несколько операций для вывода списка, например индексацию, итерацию и нарезку. Список включает следующие функции:

  • Элементы списка заключаются в квадратные скобки, а каждый элемент отделяется запятой(,).
  • Это изменяемый тип, что означает, что мы можем изменять элементы списка после их создания.
  • Списки упорядочены, элементы хранятся в определенном порядке. Мы можем использовать индексацию для доступа к элементу списка.
  • Мы можем хранить элементы с разными типами данных и комбинировать строки, целые числа и объекты в одном списке.
Читайте также:  Html div at the bottom

Ниже приведены примеры списка.

list = [31, 60, 19, 12] print(list) print(type(list))
# creating a list containing elements # belonging to different data types list1 = [1,"Yash",['a','e']] print(list1)

В приведенном выше списке первым элементом является целое число; второй – это строка, а третий – список символов.

Массивы

Массив в Python – это также линейная структура данных, в которой хранятся данные. Он также упорядочен, изменяем и заключен в квадратные скобки. Он может хранить неуникальные элементы. Но есть ограничения на хранение значений разных типов данных.

Для работы с массивом в Python нам нужно импортировать либо модуль массива, либо Numpy.

import array as arr or import numpy as np

Элементы размещаются в непрерывной области памяти, что позволяет нам легко изменять, добавлять, удалять и получать доступ к элементу. Более того, нам нужно указать тип данных. Давайте разберемся в следующих примерах.

Import array as arr array_1 = arr.array("i", [31, 60,19, 12]) print(array_1) print(type(array_1))

Пример – 2: Использование массива Numpy

import numpy as np array_sample = np.array(["str", 'sunil', 'sachin', 'megha', 'deepti']) print(array_sample) print(type(array_sample))
[‘numbers’ ‘sunil’ ‘sachin’ ‘megha’ ‘deepti’]

Мы указали тип строки и сохранили строковое значение.

Разница между массивом и списком

Здесь мы обсудим различия между массивом и списком.

Список Массив
1. В списке могут храниться значения разных типов. Он может состоять только из значений одного типа.
2. Список не может обрабатывать прямые арифметические операции. Массив может напрямую обрабатывать арифметические операции.
3. Списки представляют собой встроенную структуру данных, поэтому нам не нужно их импортировать. Перед работой с массивом нам необходимо импортировать его модуль.
4. Списки менее совместимы, чем массивы для хранения данных. Массивы более совместимы, чем список.
5. Он потребляет большой объем памяти. Это более компактный по объему памяти по сравнению со списком.
6. Подходит для хранения более длинной последовательности элементов данных. Подходит для хранения более короткой последовательности элементов данных.
7. Мы можем распечатать весь список, используя явный цикл. Мы можем распечатать весь список без использования явного цикла.
8. Он может быть вложенным, чтобы содержать различные типы элементов. Он может содержать любые вложенные элементы одинакового размера.

Мы обсудили различия между массивом и списком. Оба типа данных важны в Python, и у обоих есть некоторые ограничения. Массивы обычно используются для анализа данных.

Источник

Оцените статью