Example
list = ["apple", "banana", "cherry"]
print(list)
output:
['apple', 'banana', 'cherry']
In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.).
The syntax for creating a list is:
<list_name>=[ ]
list types and examples:
list 1=[10,20,30,40]
list 2=[1, "Hello", 3.4]
list 3=['a','e','i','o','u']
list 4= [ ]
list 5=["nellore","venkatagiri"]
list 6 = ["mouse", [8, 4, 6], ['a']]
You access the list items by referring to the index number:
Example:
list = ["apple", "banana", "cherry"]
print(list[1])
output:
banana
Negative indexing means beginning from the end, -1
refers to the last item, -2
refers to the second last item etc.
Example:
list = ["apple", "banana", "cherry"]
print(list[-1])
output:
cherry
Traversing is an operation on the data structures. It is the process where you access each and every element present in a data structure like an array or a linked list or any data structure for that matter.
You can loop through the list items by using a for
loop:
Example:
list = ["apple", "banana", "cherry"]
for i in list:
print(i)
output:
apple
banana
cherry
The range() is a built-in function of Python which returns a range object, which is nothing but a sequence of integers. i.e., Python range() generates the integer numbers between the given start integer to the stop integer, which is generally used to iterate over with for loop.
python allows you to compare two lists.each element is individually compared in lexicographical [alphabetical or dictionary] order.we can apply all the relations operators to compare two lists if they are of comparable type,otherwise python flashes an error.