Python list
In this article we will be discussing about python list in detail.It covers everything about python lists.the topics to be discussed in python lists are:
What is Python list?
How List is different from tuple?
How to use Nested lists in python?
How to do Iterations over python lists using For loop?
What is the use of build-in range function in python lists?
How to use While loop in python lists?
Along with these topics we will discuss some more features of python lists like:
Assigning and copying lists
List Comprehensions
What is Python list?
A list is a Linear Data Structure used to store the elements in linear order. Index values are used to identify items in a list. Zero-based indexing is used in the python which means indexing starts from 0 to n-1 where n is the total number of elements in a list. Python lists are Mutable which means they can be altered.there can be mixed type of elements in a list like integers, strings, float etc. A list is enclosed in square brackets and the elements are separated by comma(,) as shown below,
[ apple, banana, orange ] [10, 20, 30 ] [apple, 50, banana, 4.5 ]
An empty pair of square brackets is called Empty list [] .
How to access elements of a lists?
Index values within square brackets are used to access the values of a list.for example if we have a list fruits = [ apple, banana, orange ]
then to access its elements we use the syntax:
fruits[0] → apple accessing first element
fruits[1] → banana accessing second element
fruits[2] → orange accessing third element
to print the elements of lists ,the syntax is:
print(list) it will print all the elements in the list
print(fruits[2]) it will print the element at 2nd index of list fruits
to add the elements of a list, the syntax is:
lst = [10, 20, 30 ] → sum = lst[0] + lst[1] + lst[2]
Operations to modify and alter the lists:
Update: lst[2] = 40 it will replace the value 30 with 40 at 2nd index as [10, 20, 40]
Delete: del lst[2] it will delete the element at 2nd index of list lst as [10, 20]
Insert: lst.insert (1, 3) it will insert 3 at index 1 as [10, 3, 40]
Append: lst.append(50) it will append 50 into the list as [10, 3, 40, 50]
Sort: lst.sort() it will sort the lst in ascending order as [3, 10, 40, 50]
Reverse: lst.reverse() it will reverse the elements of list as [50, 40, 10, 3]
How List is different from tuple?
Tuple is immutable which means unlike lists it cannot be altered and this is the only difference between lists and tuples otherwise they are same.we denote tuples in round brackets instead of square brackets as shown below:
mytuple = (apple, 50, banana, 4.5 )
one more difference is that we separate a singe element tuple by comma which is not required in lists.
mylist = (1 ) → Wrong
mylist = (1, ) → Right
An empty tuple is represented by an empty pair of parenthesis () and elements of tuple are also accessed by using index in square brackets in the same way as in lists. we cannot apply any operation to modify a tuple as it is invalid.
some more operations for Lists and Tuples in python:
newlist = [10, 20, 30 ] / newlist = (10, 20, 30 )
To find length: len(newlist) → 3
To select an element: s[0] → 10
To slice: slice[0:2] →[10,20]
to count: newlist.count(2) → 30
To find index: newlist.index(20) → 1
Membership: 40 in newlist → false
Concatenation: newlist + lst → [10,20,30,10,20,30] / (10,20,30,10,20,30)
Minium value: min(newlist) → 10
Maximum Value: max(newlist) → 30
Sum: sum(newlist) → 50
How to use Nested lists in python?
lists and tuples can take any type of elements. lists and tuples can be nested to create even more complex data structures by using nested lists.In simple words ,Nested list means List inside a list.Yes, there can be lists inside a list also.They are called sub lists.Sub lists in a list are denoted by round brackets and separated by comma as shown.
[ ( 2,3,4) , (4,7,10) , (6, 6.5,7.2)]
here first sub-list i at index[0], second on index[1] and so on.To access elements in a sub list we use index of both list and sub list. here is a list of marks of 3 students of different subjects. To access the marks of all students in first subject we will use syntax:
list = [ ( 2,3,4) , (4,7,10) , (6, 6.5,7.2)]
list([0][0]) → 2 accessing first element of first sub list
list([1][0]) → 4 accessing first element of second sub list
list([2][0]) → 6 accessing first element of third sub list.
print(list[1][2]) → 7 it will print the element at 2nd index of the sub list at first index of the list
Example: Calculating average marks of first exam all the students of a class using while loop which iterates over the marks of first exam.
sum = 0
i = 0
while i < len(list):
sum = sum + list [i][0]
i+=1
average = sum / len(list)
How to do Iterations over python lists using For loop?
A for loop is an iterative control statement which iterates once for each element for a specified set of elements.They construct definite loops i.e specified number of elements.suppose there are 6 elements in a list so our loop will also iterate exactly 6 times.
Example: num = [10,20,30,40,50,60]
for i in num:
print (i)
it will print all the elements in the list num.it automatically iterates on all the elements of the list unlike while loop where we need to initialize and increment.next we will see the use of build-in range function in loops.
What is the use of build-in range function in python lists?
Build-in range function in python is used to generate a sequence of numbers in for loop to iterate over.it is used for large list of elements as it saves memory. Actually it creates a generator function which is able to produce next values of sequence when required not a sequence of numbers.
Example: sum = 0
for i in range (0,11)
sum = sum + 1
print(sum)
it will generate the values from starting number but not including the last number specified in the range.for example for range (1,11) it will generate values [1,2,3,4,5,6,7,8,9,10] i.e from 1 to 10. by default this function produces a consecutive range of numbers but we can skip the numbers in between and can even start the sequence backwards.
range(1,11,2) → [ 1, 3, 5, 7, 9]
range(10,0,-1) → [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
How to use While loop in python lists?
While loops are used in a situation when we need to traverse the list while a certain condition is true.
in these type of situations it is the best control structure to use.
let us say we need to check whether number 40 exists in a list [10, 20, 30] when the element will not found in the list then loop will terminate.
Example of while loop:
list = [1, 2, 3 ,4 ,5, 6]
i= 0
while i< len(list):
print (list[i])
i= i+1
Assigning and copying lists
when a variable is assigned to another variable holding a list then both variable end up refering to the same instance of list in the memory.
if we take an example of list2 = list1 then [1,3,6,37,11,53]←list1
↑
list2
Both variables are referring to the same list in the memory.If we change the element of list1 then then those changes will occur in list2 also.
>>> list1 = [1,3,6,37,11,53]
>>>list2 = list1
>>> list1[1] = 4
>>> list1
>>> [1,4,6,37,11,53] Value is updated at index[1] of list1
>>> list2
>>> [1,4,6,37,1,53] changes are also occurring in list2
This will not be applicable on strings and tuples as they are immutable and we cannot make changes to them.we can even make copy of the list using syntax list2= list(list1)
In this case the outputs will be:
>>> list1 = [1,2,6,7]
>>> list2 = list(list1)
>>> list1 [1]= 4
>>> list1
>>> [1,4,6,7] values of list1 are modifed
>>> list2
>>> [1,2,6,7] values of list2 are same
when we copy lists having sub lists that is called deep copying.
List Comprehensions
With range function we can only generate sequence of integers with fixed increments but list comprehensions allows us to generate even more varied sequences.let us see some examples of list comprehensions:
[ x**2 for x in (2, 3, 4)] → [4, 9, 16]
this generated square of the numbers given.
[x for x in range (0, 5)] →[0, 1, 2, 3, 4]
this will generate numbers in a specified range.
list = [-1,3,-6,4,7]
[x for x in list if x >= 0] →[3, 4, 7]
this will generate a list of positive numbers i.e numbers greater than 0.

I found some useful information in your blog, it was awesome to read, thanks for sharing
ReplyDeletePython Online Training in Hyderabad
Python Training in Hyderabad
Python Training
Python Online Training
It’s actually a great and helpful piece of info. I’m happy that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing! Best python list sort service provider.
ReplyDeleteThe Best Slots | Casino Roll
ReplyDeleteThe best slots novcasino at Casino Roll. If you love table games, to play sol.edu.kg blackjack, you have to bet twice for ventureberg.com/ the dealer 바카라 사이트 to win. The dealer must 토토 사이트 홍보