Class list in Python
The class list in Python is an array that does not have a fixed size, it can store values of different types simultaneously and you can change their values whenever you want.
method of definitionlist in python
To definelist in python we use the symbol [ ].
Inside this code you can pass values directly to it provided you put a comma between every two elements. And you can only specify the type and number of items you want to put in it.
In the following example, we have defined listempty, that is, it does not contain any element.
Empty definition method list
A = [] # A is empty named list here we have defined print(A) # (that is, as we defined it) as A Here we have shown what the object contains
• We will get the following result when running.
In the following example, we have defined listand put integers in it.
How to define list integers
numbers = [10, 20, 30, 40, 50] # Contains integers only numbers whose name is list Here we have defined print(numbers) # ( as we defined it ) as numbers Here we have displayed the content of the object
• We will get the following result when running.
In the following example, we have defined listand placed text in it.
How to define list it in texts
names = ['Rami', 'Sara', 'Nada'] # Contains text only names Its name is list Here we have defined print(names) # (that is, as we defined it) as it is names Here we have shown what the object contains
• We will get the following result when running.
In the following example, we have defined listand put integers and texts in it.
How to define list integers and texts
data = [1, 'Mhamad', 'Harmush', 1500] # contains integers and strings data named list here we have defined print(data) # (that is, as we defined it) as data Here we have shown what the object contains
• We will get the following result when running.
In the following example, we have defined listits name dataand it consists of 4 elements. Then we gave it 4 different values in type.
Note: the word Nonewe set to indicate that we do not want to set default values for the elements inhgj list.
Fifth example
# Consists of 4 elements, the default value is None, the data name is list Here we have defined data = [None] * 4 # Here we put a different value in each element in it data[0] = 100 data[1] = 5.5 data[2] = True data[3] = 'Hello' # data Here we have shown what the object contains print(data)
• We will get the following result when running.
In the following example we have defined listits name numbers, its default values are0,And it consists of 3 elements, then we give it 3 integers.
Sixth example
# It consists of 3 elements with a default value of 0, numbers whose name is list. Here we have defined numbers = [0] * 3 # Here we put a different value in each element in it numbers[0] = 10 numbers[1] = 15 numbers[2] = 20 # numbers Here we have shown what the object contains print(numbers)
• We will get the following result when running.
In the following example, we have defined list, its name languages, the values of its default elements are'Not Specified',And it consists of 3 elements, then we give it 3 text values.
Seventh example
# 'Not Specified' consists of 3 elements whose default is languages, whose name is list. Here we have defined languages = ['Not Specified'] * 3 # Here we put a different value in each element in it languages[0] = 'Arabic' languages[1] = 'English' languages[2] = 'French' # languages Here we have shown what the object contains print(languages)
• We will get the following result when running.
Accessing Objects listin Python
To access any element in list Python , whether to get or change its value, we use the number .Indexof the element.
How to access list elements in Python To access any element in the list, whether to get its value or change it, we use the element's index number.
In Python, you can access elements inthelistIn two ways:
If you want to access itemsthe
listFrom left to right, i.e. of the first element entered, theIndexFor the first element it will be 0 .If you want to access itemsthe
listFrom right to left, i.e. of the last element entered, theIndexThe last element will be 1- .
In the following example, we have defined listand placed text in it. Then we display the value of the first and second element in it.
first example
# We put text values in it that represent the names of people whose name is list Here we have defined names = ['Rami', 'Sara', 'Nada', 'Mhamad', 'Salem'] print(names[0]) # names Here we have shown the value of the first element found in the object print(names[1]) # names Here we have shown the value of the second element in the object
• We will get the following result when running.
Sara
In the following example, we have defined listand placed text in it. Then we display another value and before the last element in it.
second example
# We put text values in it that represent the names of people whose name is list Here we have defined names = ['Rami', 'Sara', 'Nada', 'Mhamad', 'Salem'] print(names[-1]) # names Here we have shown the value of the last element found in the object print(names[-2]) # names Here we have shown the value before the last element of the object
• We will get the following result when running.
Mhamad
In the following example, we have defined listand placed text in it. Then we display all the values placed in it using the loop for.
third example
# We put text values in it that represent the names of people whose name is list Here we have defined names = ['Rami', 'Sara', 'Nada', 'Mhamad', 'Salem'] # and then x will be printed in the variable names every time the value of an object is set for x in names: print(x)
• We will get the following result when running.
Sara
Nada
Mhamad
Salem
In the following example, we have defined listand put integers in it. Then we calculated the sum of all the values placed in it using the loop for.
Fourth example
# We put integer numbers in it, whose name is list. Here we have defined
numbers = [10, 20, 30, 40, 50]
# numbers Here we have defined a variable that we will use to save the sum of the values in the object
total = 0
# total and then it will be added to the value of the variable x in the variable numbers every time the value of an element of the object will be set
for x in numbers:
total += x
# numbers which will be equal to the sum of the values in the total object here we have printed the value of the variable
print('Total sum is:', total)
• We will get the following result when running.
delete itemsthelistBy sentence delin Python
Sentence delis used to delete an array as it is from memory or to remove specific elements from it.
Be careful when using the del clause in Python
When an element is deleted listby sentence del, the Python interpreter rearranges its elements again and updates a numbertheIndexfor each element.
In the following example we have defined listand placed numbers in it. Then we removed two components from it.
The first example of deleting items from the class list
# We put integers in it arr named list Here we have defined arr = [10, 20, 30, 40, 50] del arr[0] # arr Here we have deleted the first element present in the object del arr[1] # arr Here we have deleted the second element in the object print(arr) # arr Here we have shown what the object contains
• We will get the following result when running.
• The following image shows how the items have been cancelled.
In the following example we have defined listand placed numbers in it. Then we deleted the items in it starting with the item that ownsIndexNumber 0 down to the element before the one that ownsIndexNo. 3 . That is, we deleted the items arr[0]- arr[1]- arr[2]all at once.
The second example of deleting items from the list class in Python
# We put integers in it arr named list Here we have defined arr = [10, 20, 30, 40, 50] del arr[0:3] # arr Here we have deleted the first three elements of the object print(arr) # arr Here we have shown what the object contains
• We will get the following result when running.
• The following image shows how the items have been cancelled.
In the following example we have defined listand placed numbers in it. Then we deleted it from memory. Then we tried to show what it contains.
third example
# We put integers in it arr named list Here we have defined arr = [10, 20, 30, 40, 50] del arr # As it is from memory arr Here we have deleted the object print(arr) # which we originally deleted from memory so it will throw an error when we run arr here we tried to display what the object contains
• We will get the following result when running.
splitlist in python
What is meant by fragmentation?thelistIt is the return of a part of it whether to display or store.
In the following example we have defined listand placed numbers in it. Then we showed part of it.
We actually displayed the items in it starting with the item that ownsIndexNumber 0 down to the element before the one that ownsIndexNo. 3 .
That is, we displayed the values of the elements arr[0]- arr[1]- arr[2].
first example
arr = [10, 20, 30, 40, 50] # we put integers in it arr named list here we have defined print(arr[0: 3]) # arr Here we have shown the first three elements of the object
• We will get the following result when running.
In the following example we have defined listand placed numbers in it. Then we copied part of it and put it in lista second.
We actually copied the elements in it starting from the one that ownsIndexNumber 0 down to the element before the one that ownsIndexNo. 3 .
That is, we copied the values of the elements arr[0]- arr[1]- arr[2].
second example
arr = [10, 20, 30, 40, 50] # we put integers in it arr named list here we have defined arr2 = arr[0:3] #arr2 and put it in a new object named arr here we copied the first three elements in the object print(arr2) # arr2 Here we have shown what the object contains
• We will get the following result when running.
Using the factors +and *and inwiththelistin python
| The Worker | Indications |
|---|---|
+ |
An operator +is used to combine one listor more into listone. |
* |
An operator *is used to iterate a specific value or data type to represent the type of values that can be placed in elementsthelistAs we did in some of the previous examples. |
in |
The operator inis used to search inthelistAbout a specific value or to pass its values when using it in the loop, foras we did in some of the previous examples. |
In the following example we store two listin listone by the operator +.
first example
arr1 = [1, 2, 3] # We put integers in it arr1 named list here we have defined arr2 = [4, 5, 6] # we put integers in it arr2 named list here we have defined arr3 = arr1 + arr2 # arr2 and arr1 we put the values of the two objects arr3 named list here we have defined print(arr3) # arr3 Here we have shown what the object contains
• We will get the following result when running.
In the following example we have defined listand put 3 elements that have 'python'as their value by the operator *.
second example
arr = ['python'] * 3 # 'python' we put 3 elements in it whose value is the text arr named list here we have defined print(arr) # arr Here we have shown what the object contains
• We will get the following result when running.
In the following example, we used the operator into search inthelistabout a certain value.
third example
arr = ['Mhamad', 'Rony', 'Rima', 'Sara'] # We put a string of values in it arr named list Here we have defined
x = 'Rima' # We put the text x here we have defined a variable named
print('Is Rima in the list?')
print(x in arr) # True if found then .arr will be shown in object x here the value will be searched for
• We will get the following result when running.
True
Functions in the class listin Python
| Function name and definition | |
|---|---|
append(obj)
Used to add a new element in anotherthelistwho summoned her.Explanation of the append function |
|
extend(iterable)
Used to add array elements of any type in anotherthelistwho summoned her.Explanation of the extend . function |
|
insert(index, obj)
Used to add a new item in a specific place inthelistwho summoned her.see example » |
|
pop([index])
Used to delete a specific item inthelistwho called it, or to delete the last item in it.see example » |
|
clear()
Used to delete all itemsthelistwho summoned her.see example » |
|
copy()
Return a copy ofthelistwho summoned her.see example » |
|
count(x)
used to search inthelistwho called it for a certain value. Returns an integer representing how many times an element has been found that has the same value as the parameter we passed x.see example » |
|
remove(x)
Used to delete the first item found inthelistThe one who called it has the same value that we pass it to in place of the parameter x. If the value to be deleted is not found, an exception is thrownValueError . see example » |
|
index(x[, start[, end]])
search inthelistwho summoned herIndexThe first element that has the value we pass to it is in place of the parameter xand returns it. If the value you want to know is not found, an exception is thrownValueError . see example » |
|
sort(reverse=False)
used to arrange itemsthelistwhich is called in ascending or descending order.see example » |
|
reverse(reverse=False)
Used to reverse the order of elementsthelistwho summoned her.see example » |
|
Functions are ready in Python to handlethe list in python
| Function name and definition | |
|---|---|
len(list)
Returns an integer representing the number of elementsthelistwhich we pass to her when she is summoned.see example » |
|
min(list)
Returns the smallest value in .thelistwhich we pass to her when she is summoned.see example » |
|
max(list)
Returns the largest value inthelistwhich we pass to her when she is summoned.see example » |
|
list(sequence)
Returns a copy of any object containing a set of elements that we pass to it when called as a class objectThe list.see example » |
|
python list function append() in Python
its definition
Used to add a new element in anotherthelistwho summoned her.
python list append() function in Python Define it Used to add a new item to the end of the list that called it.
built
list.append(x)
parameters
In place of the parameter xwe pass the object we want to add in anotherthelist.
Return value
It does not return a value.
Example
aList = ['Apple', 'Banana', 'Mango'] # We put 3 aList elements in it named list Here we have defined
print('Before:', aList) # aList Here we have shown what the object contains
aList.append('Orange') # aList Here we have added text as a new element at the end of the object
print('After: ', aList) # Again aList Here we have shown what the object contains
• We will get the following result when running.
After: ['Apple', 'Banana', 'Mango', 'Orange']
Function() extend in Python
its definition
Used to add array elements of any type in anotherthelistwho summoned her.
built
list.extend(iterable)
parameters
In place of the parameter iterable, we pass an object representing the array whose elements we want to add in anotherthelist.
Return value
It does not return a value.
Example
list1 = [1, 2, 3] # We put 3 elements in list1 named list here we have defined
list2 = [4, 5, 6] # We put 3 items in it, named list2. Here we have defined
list1.extend(list2) # list1 in the last of the object list2 here we have added the objects of the object
print('list1 contains:', list1) # list1 Here we have shown what the object contains
• We will get the following result when running.
python list () function insert in Python
its definition
Used to add a new item in a specific place inthelistwho summoned her.
built
list.insert(index, obj)
parameters
The place of the parameter
indexwe pass a number representingtheIndexat which the object will be addedobjinThelist.Where the parameter is
objwe pass the object we want to add inThelist.
Return value
It does not return a value.
first example
# We put 3 aList elements in it named list here we have defined
alist = [1, 2, 3]
# ( index = 1 ) and we have added it in the place of the element that has alist here we have added the value 7 as a new element on the object
alist.insert(1, 7)
# alist Here we have shown what the object contains
print('alist contains:', alist)
• We will get the following result when running.
second example
# We put in each of them 3 list elements here we have defined two
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# list1 in the last of the list2 object here we have added the objects of the object
list1.insert(1, list2)
# list1 Here we have shown what the object contains
print('list1 contains:', list1)
• We will get the following result when running.
python list pop() function in Python
its definition
Used to return the value of a specified element inthelistThe one who called it and then deleted it, or to return the value of the last element that was in it and then deleted it.
When called if you pass a number that representsIndexIf you want to delete it, it returns a value and deletes that particular item.
And if you call it without passing any number to it, it returns the value of the last element found and then deletes it.
built
list.pop([index])
parameters
xIt is an optional parameter, you can pass in its place a number representingIndexThe item you want to get its value and then delete it fromthelist.
Return value
Returns the item that was deleted fromThe list.
first example
# We put 5 aList elements in it named list here we have defined
alist = [10, 20, 30, 40, 50]
# and display its value alist here we have extracted the last element in the object
print('Returned element:', alist.pop())
# alist Here we have displayed the remaining elements in the object
print('Remaining elements:', alist)
• We will get the following result when running.
Remaining elements: [10, 20, 30, 40]
second example
# We put 5 aList elements in it named list here we have defined
alist = [10, 20, 30, 40, 50]
# and display its value ( index = 1 ) Here we extract the element that owns
print('Returned element:', alist.pop(1))
# alist Here we have displayed the remaining elements in the object
print('Remaining elements:', alist)
• We will get the following result when running.
Remaining elements: [10, 30, 40, 50]
python list() function clear in Python
its definition
Used to delete all itemsthelistwho summoned her.
built
list.clear()
parameters
Do not accept any parameters.
Return value
It does not return a value.
Example
# We put 5 aList elements in it named list here we have defined
alist = [10, 20, 30, 40, 50]
# alist Here we have deleted all the elements in the object
alist.clear()
# Empty list which will appear as alist Here we have shown what the object contains
print('aList contains:', alist)
• We will get the following result when running.
python list() function copy in Python
its definition
Return a copy ofthelistwho summoned her.
built
list.copy()
parameters
Do not accept any parameters.
Return value
Return a copy ofthelistwho summoned her.
Example
# We put 5 elements in list1 named list here we have defined
list1 = [10, 20, 30, 40, 50]
# list2 in the object list1 here we have copied the objects of the object
list2 = list1.copy()
# list2 and list1 here we have shown what the object contains
print('list1 contains:', list1)
print('list2 contains:', list2)
• We will get the following result when running.
list2 contains: [10, 20, 30, 40, 50]
python list() function count in Python
its definition
used to search inthelistwho called it for a certain value.
Returns an integer representing how many times an element has been found that has the same value as the parameter we passed x.
built
list.count(x)
parameters
Where the parameter is xwe pass the object or value we want to find inThe list.
Return value
Returns an integer representing how many times an element has been found inthelistThe one who called it has the same value that we passed to it in place of the parameter x.
Example
# We put in it an array of integers called aList, here we have defined
alist = [1, 3, 8, 4, 3, 3, 7, 2, 3, 4, 3]
# found and then we put the result in the variable alist to return how many times the value '3' is found in the count() object here we called the function
found = alist.count(3);
# found Here we have shown the value in the variable
print('Number 3 exists', found, 'in the list')
• We will get the following result when running.
python list() function remove in Python
its definition
Used to delete the first item found inthelistThe one who called it has the same value that we pass it to in place of the parameter x.
built
list.remove(x)
parameters
position of the parameterobj we pass the object or value we want to delete inThe list.
Return value
Returns an integer representing how many times an element has been found inthelistThe one who called it has the same value that we passed to it in place of the parameter obj.
Possible errors
Throws a ValueError if the value to be deleted is not found.
first example
# We put in it an array of integers called aList, here we have defined alist = [1, 2, 3, 4, 5, 1] # 'has the value '1 aList Here we have deleted the first element of the object alist.remove(1) # alist Here we have shown what the object contains print(alist)
• We will get the following result when running.
second example
# We put in it an array of integers called aList, here we have defined alist = [1, 2, 3, 4, 5, 1] # When running ValueError it has the value '6' and since there is no element with this value the aList error will appear to delete the first element in the object remove() here we called the function alist.remove(6) # alist Here we have shown what the object contains print(alist)
• We will get the following result when running.
python list() function index in Python
its definition
search inthelistwho summoned herIndexThe first element that has the value we pass to it is in place of the parameter xand returns it.
If the value to be searched is not found, an exception is thrownValueError .
built
index(x[, start[, end]])
parameters
xAn expression for the value or object we want to find.startOptional parameter, you can pass in place an integer specifying a numbertheIndexWhere do you want to start your search?endOptional parameter, you can pass in place an integer specifying a numbertheIndexWhich you want to stop searching before.
Return value
return numberIndexThe first element that has the value we pass to it is in place of the parameter x.
Possible errors
Throws a ValueError if the value you want to know is not found.
Example
# We put in it an alist group of integers called list. Here we have defined alist = [1, 2, 3, 4, 5] #3 The first element that has the value index here we have shown print(alist.index(3)) # alist is the first element that has the value 3 and we start the search process from the second element in the index object here we have shown print(alist.index(3, 1)) # Until the last element in which alist is found, the first element has the value 3 and we start the search process from the second element in the index object here we have shown print(alist.index(3, 1, 5)) # When running ValueError the first element that has the value 10. Since there is no element that has this value, the index error will appear here. We have shown print(alist.index(10))
• We will get the following result when running.
2
2
ValueError: 10 is not in list
python list() function sort in Python
its definition
used to arrange itemsthelistwhich is called in ascending or descending order.
If you don't pass any value to it when called, it will sort the items in ascending order.
built
list.sort(reverse=False)
parameters
reverseIt is an optional parameter, you can pass its valueTrue in case you want to arrange the elementsthelistdescending.
Return value
It does not return a value.
first example
# We put in it an array of integers called aList, here we have defined
alist = [3, 4, 1, 5, 7, 2]
# aList Here we have shown what the object contains
print('Before sorting:', alist)
# in ascending order, i.e. from smallest to largest aList to sort the elements of the sort() object here we called the function
alist.sort()
# Again to see the difference aList here we have shown what the object contains
print('After sorting: ', alist)
• We will get the following result when running.
After sorting: [1, 2, 3, 4, 5, 7]
second example
# We put in it an array of integers called aList, here we have defined
alist = [3, 4, 1, 5, 7, 2]
# aList Here we have shown what the object contains
print('Before sorting:', alist)
# In descending order, from largest to smallest aList to sort the elements of the sort() object here we called the function
alist.sort(reverse=True)
# Again to see the difference aList here we have shown what the object contains
print('After sorting: ', alist)
• We will get the following result when running.
After sorting: [7, 5, 4, 3, 2, 1]
python list() function reverse in Python
its definition
Used to reverse the order of elementsthelistwho summoned her.
built
list.reverse(reverse=False)
parameters
Do not accept any parameters.
Return value
It does not return a value.
Example
# We put in it an array of integers called aList, here we have defined
alist = [3, 4, 1, 5]
# aList Here we have shown what the object contains
print('Before reversing:', alist)
# aList to reverse the order of the object's elements reverse() here we called the function
alist.reverse()
# Again to see the difference aList here we have shown what the object contains
print('After reversing: ', alist)
• We will get the following result when running.
After reversing: [5, 1, 4, 3]
python list len() function in Python
its definition
Returns an integer representing the number of elementsthelistwhich we pass to her when she is summoned.
built
len(list)
parameters
The location of the parameter listwe passthelistfor which we want to get the number of its elements.
Return value
Returns an integer representing the number of elementsthelistwhich we pass to her when she is summoned.
Example
# We put in it an array of integers called aList, here we have defined
alist = [1, 2, 3, 4, 5]
# len() that the aList function will return here we have shown the number of elements of the object
print('Array length is:', len(alist))
• We will get the following result when running.
python list min() function in Python
its definition
Returns the smallest value inthelistwhich we pass to her when she is summoned.
built
min(list)
parameters
The location of the parameter listwe passthelistin which we want to get the smallest value.
Return value
Returns the smallest value inthelistwhich we pass to her when she is summoned.
Example
# We put in it an array of integers called aList, here we have defined
alist = [5, 2, 4, 6, 3]
# min() which the aList function will return here we have shown the smallest value in the object
print('Minimum value is:', min(alist))
• We will get the following result when running.
python list() function max in Python
its definition
Returns the largest value inthelistwhich we pass to her when she is summoned.
built
max(list)
parameters
The location of the parameter listwe passthelistWhich we want to get the most value in.
Return value
Returns the largest value inthelistwhich we pass to her when she is summoned.
Example
# We put in it an array of integers called aList, here we have defined
alist = [5, 2, 4, 6, 3]
# max() which the aList function will return here we have shown the largest value in the object
print('Maximum value is:', max(alist))
• We will get the following result when running.
Function() list in Python
its definition
Returns a copy of any object containing a set of elements that we pass to it when called as a class objectThe list.
built
list(sequence)
parameters
In place of the parameter sequence, we pass an object that represents an object that represents an array of elements.
Return value
Returns a copy of any object containing a set of elements that we pass to it when called as a class objectThe list.
first example
# We put in it a set of integers called aSet. Here we have defined
aSet = {1, 2, 3, 4, 5}
# aList that you will return in the list object Then we store the .list object to return a copy of it as an object of the aSet class on the object list() here we called the function
aList = list(aSet)
# aList Here we have shown what the object contains
print(aList)
• We will get the following result when running.