Sunday, July 23, 2017

List in python - Introduction

Python list class allows you to create a mutable sequence of elements. List can contains mix of objects, i.e. can contain integer, string, float, double etc.
Example:
# List Example
int_list = [1,2,3,4,5] # contains only integer Value
aplhabet_list =['a','b','c','d','e'] # contains alphabets
alphanumeric_list =['a','b','100','c','200','python-2.7.12'] # List with mix of different type element

You can create list out of a string which contains each character of string as it’s element.
Example:
#Convert string into List
myString = 'python'
myList = list(myString) # Using list constructor function converting string in to list
print myList

>>> ['p', 'y', 't', 'h', 'o', 'n'] #Each character in string is stored in list as individual element

Similarly a tuple can be converted in to list.
Example:
#converting tuple in to list
myTuple = (1,2,3,4,(1,2,3)) # Tuple within tuple >> Here 1,2,3 is own tuple inside of other tuple
myTupleList = list(myTuple) # Using list constructor function converting tuple in to list. In this case myTuple is an argument to list constructor function.
print myTupleList

>>> [1, 2, 3, 4, (1, 2, 3)] # The result contains numeric element and a tuple element.

Basic List operations:

Membership: Like string, we can check the membership of elements within a given list using “in” operator.
Example:
# Testing membership of element within list
ExampleList = [1,2,3,4,5,'a','b','python']
print 1 in ExampleList
>>> True # Return True since 1 present in list.
print 10 in ExampleList
>>> False #Return False since 10 does not present in list
print 'xyz' not in ExampleList # Use of not in operator
>>> True # Return True since ‘xyz’ is not present in list.
Length: You can get the length of a list using len(<Object>) function.
# Length of a list
ExampleList = [1,2,3,4,5,'a','b','python']
print len(ExampleList)
                >>> 8 # Return length of list, which is 8.
       print len(['a','b','c'])
       >>> 3 #Return length of list, which is 3.

Concatenation of lists: We can concatenate two lists similar to string using “+” operator.
#List Concatenation
l1 = [1,2,3,4]
l2 =[1,2,3,4,5]
l3 = ['a','b','c','d']
l4 = ['d','e','f']
print l1+l2+l3+l4
>>> [1, 2, 3, 4, 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd', 'd', 'e', 'f']
Note: From above example you can see duplicate elements are not removed. In this case elements 1,2,3,4 and ‘d’ are duplicate entries in final list. Using “set” we can overcome this, which we will cover later.

Repetition of element: Repeat the same element by given number of time similar to string using “*” operator.
#Repetition of value in list
RepetionOfList =["Python"]*2
print RepetionOfList
>>> ['Python', 'Python']
# Same element repeated two times.

Iteration to access each element with list: We can access each element of list by iterating through given list.
#Iterate over lists
IterationOverList = [1,10,20,30,'python','2.7.12']
for i in IterationOverList: # Iterate through each element within list
       print i,
>>> 1 10 20 30 python 2.7.12 # Each element present in given list printed

No comments:

Post a Comment