In this tutorial you will learn about the Python List Operations and its application with practical example.
Python List Operations
In Python, along with the operations like indexing, slicing and accessing, following operations can also be applied on Python list sequences.
Table Of Contents−
Python List Concatenation
In Python, plus(+) operator can also be used to combine two lists.
Example:-
1 2 3 4 |
clr1=['red', 'green'] clr2=['blue', 'white', 'yellow'] clr3=clr1+clr2 print(clr3) |
Output:-
1 |
['red', 'green', 'blue', 'white', 'yellow'] |
Python List Repetition
In Python, * operator can also be used to combine two lists.
Example:-
1 2 3 4 |
clr1=['red', 'green'] clr2=['blue', 'white', 'yellow'] clr3=clr1*3 print(clr3) |
Output:-
1 |
['red', 'green', 'red', 'green', 'red', 'green'] |
Python List Membership
In Python, in keyword can be used to check the existence of an item in a given list.
Example:-
1 2 3 4 5 |
clr1=['red', 'green'] clr2=['blue', 'white', 'yellow'] clr3=clr1+clr2 res='red' in clr3 print res |
Output:-
1 |
True |
Python List Iteration
In Python, for-in statement makes it easy to loop over the items in a given list.
Example:-
1 2 3 |
colors= ['red', 'green', 'blue', 'white', 'yellow'] # list of 5 strings. for clr in colors: print "I like",clr |
Output:-
1 2 3 4 5 |
I like red I like green I like blue I like white I like yellow |