Chapter: Problem Solving and Python Programming : Lists, Tuples, Dictionaries

Lists - Python

List is an ordered sequence of items. Values in the list are called elements / items.

Lists

·                  List is an ordered sequence of items. Values in the list are called elements / items.

·                  It can be written as a list of comma-separated items (values) between square brackets[ ].

·                  Items in the lists can be of different data types.

 

Operations on list:

1.              Indexing

2.              Slicing

3.              Concatenation

4.              Repetitions

5.              Updating

6.              Membership

7.              Comparison

 


 

List slices:

List slicing is an operation that extracts a subset of elements from an list and packages them as another list.

 

Syntax:

Listname[start:stop]

Listname[start:stop:steps]

 

v       default start value is 0

v       default stop value is n-1

v       [:] this will print the entire list

v       [2:2] this will create a empty slice


 

List methods:

 

v     Methods used in lists are used to manipulate the data quickly.

v     These methods work only on lists.

v     They do not work on the other sequence types that are not mutable, that is, the values they contain cannot be changed, added, or deleted.

 

syntax:

list name.method name( element/index/list)



 

List loops:

1.              For loop

2.              While loop

3.              Infinite loop

 

1. List using For Loop:

v       The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects.

v       Iterating over a sequence is called traversal.

v       Loop continues until we reach the last item in the sequence.

v       The body of for loop is separated from the rest of the code using indentation.

 

Syntax:

for val in sequence:

 


 

2. List using While loop

v       The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.

v       When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

 

Syntax:

while (condition):

body of while

 

Sum of elements in list

a=[1,2,3,4,5]

i=0

sum=0

while i<len(a):

sum=sum+a[i]

i=i+1

print(sum)

Output:

15

 

3. Infinite Loop

A loop becomes infinite loop if the condition given never becomes false. It keeps on

running. Such loops are called infinite loop.

 

Example

a=1

while (a==1):

n=int(input("enter the number"))

print("you entered:" , n)

 

Output:

Enter the number 10

you entered:10

Enter the number 12

you entered:12

Enter the number 16

you entered:16

 

Mutability:

v       Lists are mutable. (can be changed)

v       Mutability is the ability for certain types of data to be changed without entirely recreating it.

v       An item can be changed in a list by accessing it directly as part of the assignment statement.

v       Using the indexing operator (square brackets[ ]) on the left side of an assignment, one of the list items can be updated.


 

Aliasing(copying):

v       Creating a copy of a list is called aliasing. When you create a copy both list will be having same memory location. changes in one list will affect another list.

v       Alaising refers to having different names for same list values.


v       In this a single list object is created and modified using the subscript operator.

v       When the first element of the list named “a” is replaced, the first element of the list named “b” is also replaced.

v       This type of change is what is known as a side effect. This happens because after the assignment b=a, the variables a andb refer to the exact same list object.

v       They are aliases for the same object. This phenomenon is known as aliasing.

v       To prevent aliasing, a new object can be created and the contents of the original can be copied which is called cloning.

 

Clonning:

v       To avoid the disadvantages of copying we are using cloning. creating a copy of a same list of elements with two different memory locations is called cloning.

v       Changes in one list will not affect locations of aother list.

 

v       Cloning is a process of making a copy of the list without modifying the original list.

1.              Slicing

2.              list()method

3.              copy() method

 

clonning using Slicing

>>>a=[1,2,3,4,5]

>>>b=a[:]

>>>print(b)

[1,2,3,4,5]

>>>a is b

False

 

clonning using List( ) method

>>>a=[1,2,3,4,5]

>>>b=list

>>>print(b)

[1,2,3,4,5]

>>>a is b

false

>>>a[0]=100

>>>print(a)

>>>a=[100,2,3,4,5]

>>>print(b)

>>>b=[1,2,3,4,5]

 

clonning using copy() method

a=[1,2,3,4,5]

>>>b=a.copy()

>>>  print(b) [1, 2, 3, 4, 5]

>>>  a is b

False

 

List as parameters:

v       In python, arguments are passed by reference.

v       If any changes are done in the parameter which refers within the function, then the changes also reflects back in the calling function.

v       When a list to a function is passed, the function gets a reference to the list.

v       Passing a list as an argument actually passes a reference to the list, not a copy of the list.

Since lists are mutable, changes made to the elements referenced by the parameter change the same list that the argument is referencing.

 

Example 1:

def remove(a):

a.remove(1)

a=[1,2,3,4,5]

remove(a)

print(a)

 

Output

[2,3,4,5]

 

Example 2:

def inside(a):

for i in range(0,len(a),1):

a[i]=a[i]+10

print(“inside”,a)

a=[1,2,3,4,5]

inside(a)

print(“outside”,a)

 

Output

inside [11, 12, 13, 14, 15]

outside [11, 12, 13, 14, 15]

 

Example 3

def insert(a):

a.insert(0,30)

a=[1,2,3,4,5]

insert(a)

print(a)

 

output

[30, 1, 2, 3, 4, 5]


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Lists, Tuples, Dictionaries : Lists - Python |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.