The data type list is an ordered sequence which is mutable and made up of one or more elements.A list can have elements of different data types, such as integer, float, string, tuple or even another list. A list is very useful to group together elements of mixed data types. Elements of a list are enclosed in square brackets and are separated by comma. List indices start from 0.
Different ways to create a list
1.
For example
>>> list1 = [2,4,6,8,10,12]
>>> print(list1)
[2, 4, 6, 8, 10, 12]
#list2 is the list of vowels
>>> list2 = [‘a’,’e’,’i’,’o’,’u’]
>>> print(list2)
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
#list3 is the list of mixed data types
>>> list3 = [100,23.5,’Hello’]
>>> print(list3)
[100, 23.5, ‘Hello’]
#list4 is the list of lists called nested
#list
>>> list4 =[[‘Physics’,101],[‘Chemistry’,202], [‘Maths’,303]]
2. List2=list(hello”)
print(list2) # outout will be [‘h’,’e’,’l’,’l’,’o’]
Accessing Elements in a List
Each individual element in a list can be accessed using a technique called indexing. The index of the first element (from left) in the list is 0 and the last element is n-1 where n is the length of the list.The element to be accessed in the list is written in square brackets ([ ]).
For example
list1 = [2,4,6,8,10,12]
print(list1[0]) #output will be 2
print(list1[5]) #output will be 12
Note:- If we give index value out of this range then we get an IndexError.
For example list1 contains 6 elements
list1 = [2,4,6,8,10,12]
print(list1[15]) # gives index error as output
The index must be an integer (positive, zero or negative).
print(list1[1.5]) # gives type error
List is mutable
A list is a mutable data type. It means that the contents of the list can be changed after it has been created.
For example
list1 = [2,4,6,8,10,12]
list1[0]= 5 # gives output as[5,4,6,8,10,12]
List Operations
Python allows certain operations on list data type, such as concatenation, repetition, membership and slicing.
Concatenation
To concatenate means to join. Python allows us to join two lists using concatenation operator plus which is denoted by symbol +.
For example
list1 = [2,4,6,8,10,12]
list2=[3,5,6]
print(list1+list2) # gives output as [2,4,6,8,10,12,3,5,6]
Note:- list can be concatenated with only list data type otherwise it generate type error
For example
print(list1+15) # gives type error as 15 is integer type
Repetition
Python allows us to repeat the given list using repetition operator which is denoted by symbol *.
For example
List1=[10,20,30]
print(2*list1) #gives [10,20,30,10,20,30]
We can write it as print(list1*2) as well
Note: list1 still remains the same after the use of repetition operator.
Membership
The membership operators in checks if the element is present in the list and returns True, else
returns False.
>>> list1 = [‘Red’,’Green’,’Blue’]
>>> ‘Green’ in list1
True
>>> ‘Cyan’ in list1
False
The not in operator returns True if the element is not present in the list, else it returns False.
>>> list1 = [‘Red’,’Green’,’Blue’]
>>> ‘Cyan’ not in list1
True
>>> ‘Green’ not in list1
FalseTrue
Slicing
In Python, to access some part of a list, we use a method called slicing. This can be done by specifying an index range.
There are two types of indexing:-
- Forward / Positive indexing:- starts from 0,1 …… so on from left to right
- Backward / Negative Indexing:- starts from -1,-2…. So on from right to left
-
Forward indexing 0 1 2 3 4 5 6 List1 2 4 6 8 10 12 14 Backward indexing -7 -6 -5 -4 -3 -2 -1
There are different ways to perform indexing:-
For example if we have a list1
List1=[2,4,6,8,10,12,14]
- When we give both lower limit and upper limit
For example
print(list1[2:6]) # gives output form indices 2 to 5, so output will be [6,8,10,12]
- When we give only lower limit
For example
print(list1[2:]) # gives output form indices 2 to till last , so output will be [6,8,10,12,14]
- When we give only upper limit
For example
print(list1[:6]) # gives output form indices 0 to 5 , so output will be [2,4,6,8,10,12]
- When we skip both lower limit as well as upper limit
For example
print(list11[:]) # gives complete list i.e [2,4,6,8,10,12,14]
- When we give difference of two from forward indexing
For example
print(list1[::2]) #will start from index 0 and then difference of two i.e [2,6,10,14]
- When we give difference of two from backward indexing
For example
print(list1[::-2]) #will start from index -1 and then difference of two i.e [14,10,6,2]
Traversing a list
List Traversal Using for Loop without range function:
list1 = [2,4,6,8,10,12]
for i in list1:
print(i,end = ”)
[2,4,6,8,10,12] #output of for loop
List Traversal Using for Loop with range function:
list1 = [2,4,6,8,10,12]
for I in range(len(str1)):
print(list1[i],end = ”)
[2,4,6,8,10,12] #output of for loop
List Traversal Using while Loop
list1 = [2,4,6,8,10,12]
i=0
while i<len(list1):
print(list1[i],end = ”)
i=i+1
list1 = [2,4,6,8,10,12]
Team NB Learner