Skip to main content

Posts

Showing posts with the label sublist

Python Hunter, Deep copy and Shallow copy by pran sukh

In any programming language the copy concept is assigning the value of variable to other variable. But python shows strange behavior when this concept is implemented, follow the examples below.       1 2 3 4 5 6 7 8 9 list1 = list () list1 = [ "Python" , "Hunter" ] list2 = list1 print (list1) print (list2) list2 [ 1 ] = "Lover" print (list1) print (list2) In the above code the list1 created and initialized and list1 is passed to list2 . but when when we change any element in list2 it will will affect the list1 too. execute the script and observe the output. Explanation:-  Because there was no new assignment to list2 , list2 was pointing to the list1 . let's see with id() 1 2 print ( id (list1)) print ( id (list2)) The output value of memory location for the both list1 and list2 will be same, that's why change in any list will reflect to other list object too. But we can re...

List, Tuple and manipulation operation on Python Hunter's blog.

   List   in python are the most flexible datatype in python. Python list looks identical to c++ and java's array (elements separated by ',' coma) but unlike in features. Like c++ and java's array hold only similar types of elements (e.g String, int, double) and these programming languages have limitation to grow the size of their declared array at run time, array size has to defined at compile time (there could be some logic to decide the array size at run by getting the user's input but, after that it will be fixed, but it is not recommended) But Python list is slightly different from c++ and Java's array as it can grow it's size at run time and hold any multiple dataType in one single list (e.g string, int, char, Boolean....... all together in one list ).      # Following are the ways to create list. Output:- ---------------------- Source Code Starts -------------------- 1 2 3 4 5 6 7 8 9 10 1...