Saturday, May 26, 2018

Python List

List: Generally speaking a list is a collection of objects that can contain any types of objects.
It can be an arbitrary mixture of elements like numbers, strings, other lists and so on.

Properties of Python lists:

  • The contain arbitrary objects like numbers, strings, other lists etc..
  • Elements of a list can be accessed by an index
  • They are arbitrarily nestable, i.e. they can contain other lists as sublists
  • They are mutable, i.e. the elements of a list can be changed


Below are the Example and operations on the list:

import copy

# creating a list 
itemlist = ['apple','orange','banana','grape']
itemlist_copy = itemlist # To copy a list (This is  Shallow copy)
#Adding new item to a exiting list 

itemlist.append('pepaya')
for item in itemlist:
    print(item)

Output:
apple
orange
banana
grape
pepaya

___________________________________________________________________________________________

# itemlist and itemlist_copy are equal because of Shallow copy
print(str(itemlist) +'==>'+str(itemlist_copy))

['apple', 'orange', 'banana', 'grape', 'pepaya']==>
['apple', 'orange', 'banana', 'grape', 'pepaya']

# Both lists are refering to the same object id.
print(str(id(itemlist)) + '==>' + str(id((itemlist_copy)))) 

4334167304==>4334167304

print(itemlist == itemlist_copy)
True
print(itemlist is itemlist_copy)
True

# this is deep copy
itemlist_copy = copy.deepcopy(itemlist) itemlist.append('Avocados') print(str(itemlist) +'==>'+str(itemlist_copy))

['apple', 'orange', 'banana', 'grape', 'pepaya', 'Avocados']==>
['apple', 'orange', 'banana', 'grape', 'pepaya']

 # Both lists are different, refering to different objects 
print(str(id(itemlist)) + '==>' + str(id((itemlist_copy))))
4334167304==>4334167048
# Another way to copy the list is using the list constructor,
as constructor used to create a new object.
itemlist_copy = list(itemlist)
# Here it is False because the list reference objects are different.
print("List copy constructor {}".format(itemlist is itemlist_copy))
List copy constructor False

# Here it is True because the list content are same in same order.
print("List copy constructor {}".format(itemlist == itemlist_copy))
List copy constructor True

# Both lists are refering to the same object id.
print(str(id(itemlist)) + '==>' + str(id((itemlist_copy)))) 
4334167304==>4334326280
# Adding two list even = [2,4,6,8] odd = [1,3,5,7,9] add = even + odd str1 = ['A','B'] str2 = ['C','D','E','FGH']print(sorted(even+odd))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

add.sort()
#It return None, becuase for sorting the values it needs objects and print do not create that
print(add.sort()) 
None
print(add)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

print(str1 + str2)
['A', 'B', 'C', 'D', 'E', 'FGH']

# You can also create a empty list by using the list constructor
l1 = []
l2 = list()print("List l1 {}".format(l1))
List l1 []

print("List l2 {}".format(l2))
List l2 []

if l1 == l2:
print("the list are equal")
the list are equal
# List constructor automatically create a list when pass a string print(list("This list is created using constructor")) ['T', 'h', 'i', 's', ' ', 'l', 'i', 's', 't', ' ', 'i', 's', ' ', 'c', 'r', 'e', 'a', 't',
'e', 'd', ' ', 'u', 's', 'i', 'n', 'g', ' ', 'c', 'o', 'n', 's', 't', 'r', 'u', 'c', 't', 
'o', 'r']


# Nested List
even = [2,4,6,8]
odd = [1,3,5,7,9]
number = [even,odd]

print(number)
[[2, 4, 6, 8], [1, 3, 5, 7, 9]]

for list_set in number:
    print(list_set)                 # Printing even and odd list    for list_evement in list_set:
        print(list_evement)         # Printing even and odd list elements
[2, 4, 6, 8]
2
4
6
8
[1, 3, 5, 7, 9]
1
3
5
7
9

No comments:

Post a Comment

Program to check valid IP addresses