LIST and ARRAY in PYTHON Flashcards
are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements
lists and arrays
Accessing element is fast in __ ___ because they are in a contiguous manner but insertion and deletion is quite expensive because all the elements are shifted from the position of inserting and deleting element linearly
Python Arrays
Accessing an element in a __ ___ is the same as an Array because a List is actually a dynamic array
Python List
in Python is an inbuilt collection of items that can contain elements of multiple data types, which may be either numeric, character logical values, etc. It is an ordered collection supporting negative indexing.
list
A list can be created using
[] containing data values
is a vector containing homogeneous elements i.e. belonging to the same data type. Elements are allocated with contiguous memory locations.
array
by using the array() function of the array module and see its type using the
type() function.
Can consist of elements belonging to different data types
List
Only consists of elements belonging to the same data type
Array
No need to explicitly import a module for the declaration
list
Can directly handle arithmetic operations
array
Cannot directly handle arithmetic operations
list
Need to explicitly import the array module for declaration
array
Greater flexibility allows easy modification (addition, deletion) of data
list
Preferred for a shorter sequence of data items
list
Preferred for a longer sequence of data items
array
Less flexibility since addition, and deletion has to be done element-wise
array
The entire list can be printed without any explicit looping
list
Consume larger memory for easy addition of elements
list
A loop has to be formed to print or access the components of the array
array
Nested ___ can be of variable size
lists
Comparatively more compact in memory size
array
Nested __ has to be of same size.
arrays
for counting a particular element in the list
count()
sort the complete list
sort()
gives maximum of the list
max()
gives minimum of the list
min()
gives sum of all the elements in list for integer list
sum()
gives first index of the element specified
index()
adds the element to the end of the list
append()
removes the element specified
remove()
are specifically used to store a collection of numeric elements that are all of the same type
Arrays
are more flexible than arrays in that they can hold elements of different types (integers, strings, objects, etc.)
Lists
my_list = [1, 2, 2, 3]
my_set = {1, 2, 2, 3} # Output will be {1, 2, 3}
list can contain duplicate element while set does not support operations that depend on sequence
An unordered collection of key-value pairs
Dictionary
Used to remove the first occurrence of a specific value from a list.
Does not return the removed element.
Raises a ValueError if the specified value is not found.
remove()
Removes an element at a specific index and returns it.
If no index is specified, __ removes and returns the last item in the list.
Raises an IndexError if the specified index is out of range.
pop or pop()
is a linear list of
elements. It is like a single row of data,
where each element can be accessed
using a single index.
one-dimensional array
can be thought of
as a table or a matrix with rows and
columns. It requires two indices to access
an element: one for the row and one for
the column.
two-dimensional array
extends beyond
two dimensions, requiring more than two
indices to access its elements. The
concept generalizes from 1D and 2D arrays
to higher dimensions, such as 3D, 4D, etc.
multidimensional array