Python List
A list in Python is used to store the sequence of various types of data. A list can be defined as a collection of values
or items of different types.
Characteristics of Lists
The lists are ordered collection
The element of the list can access by index or it is index based collection
The lists are the mutable type.
The lists are mutable types.
A list can store the number of various elements or homogenus elements
Creating a list
The items in the list are separated with the comma (,) and enclosed with the square brackets [].
Syntax:
List name=[element1,element2,element3]
Python uses the square brackets ([]) to indicate a list. The following shows an empty list:
empty_list = []
The following example defines a list of six numbers:
If you print out the list, you’ll see its representation including the square brackets. For example:
Example:
File:
Input:
numbers = [1, 3, 2, 7, 9, 4]
print(numbers)
Output:
[1, 3, 2, 7, 9, 4]
Example: The following shows how to define a list of strings
File:
Input:
colors = ['red', 'green', 'blue']
print(colors)
Code language: Python (python)
Output:
['red', 'green', 'blue']
list can contains other lists:
Example: The following example defines a list of lists
File:
Input:
coordinates = [[0, 0], [100, 100], [200, 200]]
print(coordinates)
Output:
[[0, 0], [100, 100], [200, 200]]
Ordered List Checking
Code
# example
a = [ 1, 2, "Ram", 3.50, "Rahul", 5, 6 ]
b = [ 1, 2, 5, "Ram", 3.50, "Rahul", 6 ]
a == b
Output:
False
The identical elements were included in both lists, but the second list modified the index position of the fifth
element, which is against the lists' intended order. When the two lists are compared, false is returned.
Code
# example
a = [ 1, 2, "Ram", 3.50, "Rahul", 5, 6]
b = [ 1, 2, "Ram", 3.50, "Rahul", 5, 6]
a == b
Output:
True
Lists permanently preserve the element's order. It is the arranged gathering of things because of this.
Let's have a look at the list example in detail.
Code
# list example in detail
emp = [ "John", 102, "USA"]
Dep1 = [ "CS",10]
Dep2 = [ "IT",11]
HOD_CS = [ 10,"Mr. Holding"]
HOD_IT = [11, "Mr. Bewon"]
print("printing employee data ...")
print(" Name : %s, ID: %d, Country: %s" %(emp[0], emp[1], emp[2]))
print("printing departments ...")
print("Department 1:\nName: %s, ID: %d\n Department 2:\n Name: %s, ID: %s"%( Dep1[0], Dep2[1], Dep2[0],
Dep2[1]))
print("HOD Details ....")
print("CS HOD Name: %s, Id: %d" %(HOD_CS[1], HOD_CS[0]))
print("IT HOD Name: %s, Id: %d" %(HOD_IT[1], HOD_IT[0]))
print(type(emp), type(Dep1), type(Dep2), type(HOD_CS), type(HOD_IT))
Output:
printing employee data...
Name : John, ID: 102, Country: USA
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
IT HOD Name: Mr. Bewon, Id: 11
<class ' list '> <class ' list '> <class ' list '> <class ' list '> <class ' list '>
In the above example, we have created the lists which consist of the employee and department details and printed
the corresponding details. Observe the above code to understand the concept of the list better.