What is List in Python?
For Java
developers: List is similar to arrays in Java.
For people
loyal to Python: List is used to group together various data types
such as strings, integers, float etc. It is not hashable. An object is hashable, if it has a hash value, that never changes during
it’s lifetime and can be compared to other objects.
Example:
nums=[2,8,16,95]
Where, ‘nums’ is the variable (we discussed about variables in my last
blogpost). The variable ‘nums’ stores the list containing a few integers. A list
is declared within SQUARE BRACKETS[]. The Square Brackets[] basically tell
Python that this is a list and not some other container. Containers are basically vessels which store some value in them, for example,
variables, lists etc.
We can also store different data types in a single list in the following
way:
values=[9.5,’bin’,26]
We can also group two or more lists into one list. If we add both the lists in a single list and print it , we get the following result:
String Functions in Python:
append():
The function append() in Python inserts a value at the end of the list.
Example:
(Where variable ‘nums’ is the same list declared in above examples). The
‘nums.append(36)’ command inserts the value ‘36’ at the end of the list. Now if
we print ‘nums’, it gives the following output:
insert():
The function insert() in Python insert a value in the middle of the
list. (Technically not exactly in the middle, but where the user wants it).
Example:
As you can see this function is
quite similar to the above function but it takes in two parameters instead of
one.
A
parameter is a value which is inputted by the user which is considered as an
essential value to execute a function.
Yeah so, back to the insert() function.
The command ‘nums.insert(2,3)’ takes in two parameters ‘2’ and ‘3’. The
parameter ’2’ defines the index value where you want the value to be added.
An index
value is basically a number that denotes a location of a certain piece of
information. Keep in mind, index values in Python always start with ‘0’ instead
of ‘1’. For example:
value=[1,2,3,4]
The list
‘value’ contains certain integers. Here, we have ‘1’ at index ‘0’, ‘2’ at index
‘1’, ‘3’ at index ‘2’, ‘4’ at index ‘3’. Basically, we have four elements in
this list but index value only upto ‘3’
because ‘0’ is the first index value.
So, in the command ‘insert(2,3)’, ‘2’ is the index value (or the
location where you want the value to be added) and ‘3’ is the value.
When we print the list ‘nums’, the output will now be:
remove():
This function is used to remove
a value from the list. For example:
pop():
This function is used to remove the value with the specified index
number from the list. Basically, this is similar to the remove() function but
unlike the remove() function it removes a value from a particular position in
the list. For example:
Note: If you do not specify the index value, the
last element will be removed.
del :
This function removes multiple elements from the list.
Syntax:
del <list name>(begin
index:end index)
This basically deletes the part of the list from the begin index value
specified to the end index value specified. For example:
extend():
This function adds multiple values to the list. This function works
similar to the del function but this function adds value to the list instead of
deleting them
For example:
min():
This function prints the minimum value in the list. For example:
max():
This function prints the maximum value in the list. For example:
sum():
This function prints the sum of all the values in the list. For example:
sort():
This function sorts the values in the list in ascending order. For
example:
CONGRATULATIONS!!! Now you have a brief understanding about containers in
Python. You have also learnt about your first container ‘List’ with some of
it’s functions.
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback