Python List
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 = []
Input:
numbers = [1, 3, 2, 7, 9, 4]
print(numbers)
Output:
[1, 3, 2, 7, 9, 4]
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]]
# 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