In Python, lists are defined as arrayable elements which contain heterogeneous data values. It can have any numbers of rows but a single column. These lists can be sorted, inter-changed or modified.
Explaination :
numbers = [1,2,3,4]
#visual representation
[
0 : 1, # these are values
1 : 2,
2 : 3,
3 : 4,
];
Indexing starts from zero [0], variables of different types can be stored in lists.
To create a list enter values separated by comma's in a square bracket and assign that bracket to a variable.
Accessing Lists
Example
Accessing list values are very simple and straight-forward. We simply need to specify the index of the value which we wish to use.
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
print(simple_list)
#PYTHON OUTPUT
['Jake', 25, 'New Jersey', 95255.0]
We will use the above example in creating a meaningful sentence.
Example
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
print('%s is %d years old. He stays in %s and makes $%0.2f yearly income as Python Developer '%(simple_list[0], simple_list[1],simple_list[2], simple_list[3]))
#PYTHON OUTPUT
Jake is 25 years old. He stays in New Jersey and makes
Note :
%s for strings variables
%d for interger variables
%f for float variables
They are called format specifiers.In
%0.2f
the letterf
specifies float variable and0.2
is the specifies the decimal point to be allowed.
Example
x= 25.123456
print('%0.1f'%(x))
print('%0.2f'%(x))
print('%0.3f'%(x))
print('%0.4f'%(x))
print('%0.5f'%(x))
print('%0.6f'%(x))
#PYTHON OUTPUT
25.1
25.12
25.123
25.1235
25.12346
25.123456
To update the value of a list. Assign list index in a square bracket to whichever value you would like to assign
Example
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
simple_list[0] = 'Rohit' #updating value
print(simple_list)
#PYTHON OUTPUT
['Rohit', 25, 'New Jersey', 95255.0]
Appending new value into the list using append()
method
Syntax
list.append(value)
Example
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
simple_list.append('New value added')
print(simple_list)
#PYTHON OUTPUT
['Jake', 25, 'New Jersey', 95255.0, 'New value added']
To remove a particular element from list use remove()
method.
Syntax
list.remove(value)
Example
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
simple_list.remove('New Jersey')
print(simple_list)
#PYTHON OUTPUT
['Jake', 25, 95255.0]
To empty values inside list use clear()
method
Syntax
list.clear(value)
Example
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
simple_list.clear()
print(simple_list)
#PYTHON OUTPUT
[]
To delete the list use del
keyword before list variable
Syntax
del list
Example
simple_list = ['Jake', 25, 'New Jersey', 95255.00]
del simple_list
print(simple_list)
#PYTHON ERROR OUTPUT
Traceback (most recent call last):
print(simple_list)
NameError: name 'simple_list' is not define
Iterating through lists
We will be using python's built-in for-in
loop to display list data
marks = [25,29,19,20]
for i in marks:
print(i)
#PYTHON OUTPUT
25
29
19
20
Counting the numbers of elements present in list use len()
method.
Syntax :
len(list)
Example
marks = [25,29,19,20]
print(len(marks))
#PYTHON OUTPUT
4
To reverse a given elements in list use list.reverse()
method.
Syntax :
list.reverse()
Example
marks = [25,29,19,20]
marks.reverse()
print(marks)
#PYTHON OUTPUT
[20, 19, 29, 25]
Check if the value exists in the list.
marks = [25,29,19,20]
if 25 in marks:
print("Yes exists")
else:
print("Does not exists")
#PYTHON OUTPUT
Yes exists
The above example simply returns True or False
Click to check out the complete article on python list
Top comments (0)