Lists are used to store multiple items or values in a single variable.
The list is a sequence data type which is used to store the collection
of data.TuplesandStringare other types of sequence data types
Python list are mutable, ordered, changeable, and allow duplicate
values. we can change their elements after forming.
Lists are one of 4 built-in data types the other 3 are Tuple, Set and
Dictionary, all with different qualities and usage.
Lists are created using square brackets [ ] and separated by
commas.
List items are indexed, the first item has index[0], the second item
has index[1]etc.
List Introduction:
Syntax:
list [iterable]
Example for python list:
>>> list=['a','b','c']
>>> print(list)
Output:
['a', 'b', 'c']
>>>
Example 2:
>>> list=[2,3,4,5,6]
>>> print(list)
O/P [2, 3, 4, 5, 6]
A list can contain different data types:
>>> list=["rakshita",2,"sweatha",1]
>>> print(list)
O/P ['rakshita', 2, 'sweatha', 1]
Access value in list:
There are different ways to accessing the list in python (index
and value)
Example (index value)
•
>>> list=["CS","CA","MSC"]
>>> print(list[0])
CS
>>> print(list[1])
CA
>>> print(list[2])
MSC
list.index()Return the index
value
Ex:
>>> list=["CS","CA","MSC"]
>>> list.index("CA")
O/P
1
Adding an item in the list:
append()Theappend()method adds an item at the end of
the list.
>>> list.append("AI&DS")
>>> print(list)
O/P
['CS', 'CA', 'MSC', 'AI&DS']
Insert Theinsert()method to add an element at the
specified index.
>>> list.insert(2,"MCA")
>>> print(list)
['CS', 'CA', 'MCA', 'MSC', 'AI&DS']
Basic Operation in list& List Methods :
Therearemanydifferenttypesofoperationsthat
youcanperformonPythonlists,including:
1. Append 2. Insert 3. Length 4.Sort
5. Indexing 6. Delete 7. Pop 8. Concatenation
9. Reverse 10. Extend
1. Append:
Theappend()method adds an item at the end of the list.
Example:
>>> list=["CS","CA","MSC"]
>>> print(list)
O/P
['CS', 'CA', 'MSC']
>>> list.append("AI&DS")
>>> print(list)
O/P
['CS', 'CA', 'MSC', 'AI&DS']
2.Insert:
Theinsert()method to add an element at the specified
index.
Example:
>>> list=["CS","CA","MSC"]
>>> print(list)
O/P
['CS', 'CA', 'MSC']
>>> list.insert(2,"MCA")
>>> print(list)
O/P
['CS', 'CA', 'MCA', 'MSC']
3.Length:
Pythonlen()is used to get the length of the list.
Example:
4. Sort:
The list elements can be sorted either ascending or descending.
Example:
>>> list=[]
>>> print(len(list))
O/P 0
>>> list=["CS","CA","MSC"]
>>> print(len(list))
O/P 3
>>>
a=["sweatha","priya","rakshita"]
>>> a.sort()
>>> print(a)
O/P
['priya', 'rakshita', 'sweatha']
5. Indexing:
index()is a built-in Python function that Return the index value.
Example:
6.Delete:
Using the remove function, we can easily delete a list element.
Example:
>>>list=["CS","CA","MSC"]
>>> list.index("CA")
O/P
1
>>>list=["CS","CA","MSC"]
>>> list.remove("CS")
>>> print(list)
O/P
['CA', 'MSC']
11. Max & Min:
The method used to find the minimum element in the list, and
the maximum element in the list.
Example:
>>> s1=[56,76,34,90,46]
>>> print(max(s1))
90
>>> s1=[56,76,34,90,46]
>>> print(min(s1))
34