Skip to main content

Updating, Delete Dictionary Elements & Tuples, Accessing Values in Tuples & Lists.

 Updating Dictionary

You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −


When the above code is executed, it produces the following result −

dict['Age'] : 8
dict['School'] : DPS School

Delete Dictionary Elements
You can either remove individual dictionary elements or clear the entire contents of a
dictionary. You can also delete the entire dictionary in a single operation. To explicitly remove an entire dictionary, just use the del statement. Following is a simple example −



Accessing Values in Tuples
To access values in a tuple, use the square brackets for slicing along with the index or
indices to obtain the value available at that index. For example −


When the above code is executed, it produces the following result −

tup1[0] : physics
tup2[1:5] : [2, 3, 4, 5]

Updating Tuples
Tuples are immutable which means you cannot update or change the values of tuple
elements. You are able to take portions of existing tuples to create new tuples as the
following example demonstrates −


When the above code is executed, it produces the following result −

(12, 34.56, 'abc&', 'xyz')

Delete Tuple Elements
Removing individual tuple elements is not possible. There is, of course, nothing wrong
with putting together another tuple with the undesired elements discarded.
To explicitly remove an entire tuple, just use the del statement. For example −


This produces the following result. Note an exception raised, this is because after del
tup tuple does not exist anymore −

('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File 'test.py', line 9, in <module>
print tup;
NameError : name 'tup' is not defined

Accessing Values in Lists
To access values in lists, use the square brackets for slicing along with the index or
indices to obtain value available at that index. For example −



When the above code is executed, it produces the following result −

list1[0] : physics
list2[1:5] : [2, 3, 4, 5]

Updating Lists
You can update single or multiple elements of lists by giving the slice on the left-hand
side of the assignment operator, and you can add to elements in a list with the
append() method. For example −




Note − append() method is discussed in subsequent section.

When the above code is executed, it produces the following result −

Value available at index 2 :
1997
New value available at index 2 :
2001

Delete List Elements

To remove a list element, you can use either the del statement if you know exactly
which element(s) you are deleting or the remove() method if you do not know. For
example −




When the above code is executed, it produces the following result −

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Note − remove() method is discussed in a subsequent section.







Comments

Popular posts from this blog

This program checks whether a number is a Special Number or not in JAVA

  This program checks whether a number is a Special Number or not. A number is said to be a special number when the sum of the factorial of its digits is equal to the number itself. Example - 145 is a Special Number as 1!+4!+5!=145. Program- /*import java.util.*; public class SpecialNumberCheck {      public static void main(String args[])      {           Scanner ob=new Scanner(System.in);           System.out.println(&quot;Enter the number to be checked.&quot;);           int a=ob.nextInt();           int sum=0,b=a;           while(a!=0)//or a&gt;0           {                int rem=a%10; int fact=1;                for(int i=1;i&lt;=rem;i++)      ...

Difference between Primary Key , Foreign Key & Candidate Key

 Last Updated: 23-02-2021 Primary Key: A primary key is used to ensure data in the specific column is unique. It is a column that cannot have NULL values. It is either an existing table column or a column that is specifically generated by the database according to a defined sequence. Example: Refer to the figure – STUD_NO, as well as STUD_PHONE both, are candidate keys for relation STUDENT but STUD_NO can be chosen as the primary key (only one out of many candidate keys). Foreign Key: A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It is a column (or columns) that references a column (most often the primary key) of another table. Example: Refer to the figure – STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation. Figure: Let’s see the difference between Primary Key and Foreign Key: PRIMARY KEY A primary key is used to ensure data in the specific column is unique. It uniquely identifie...

Python Methods with Description.

Python includes following list methods  1) list.append(obj) = Appends object obj to list  2) list.count(obj) = Returns count of how many times obj occurs in list  3) list.extend(seq) = Appends the contents of seq to list 4) list.index(obj) = Returns the lowest index in list that obj appears 5) list.insert(index, obj) = Inserts object obj into list at offset index 6) list.pop(obj=list[-1]) = Removes and returns last object or obj from list 7) list.remove(obj) = Removes object obj from list 8) list.reverse() = Reverses objects of list in place 9) list.sort([func]) = Sorts objects of list, use compare func if given Function with Description 1) cmp(tuple1, tuple2) = Compares elements of both tuples. 2) len(tuple) = Gives the total length of the tuple. 3) max(tuple) = Returns item from the tuple with max value. 4) min(tuple) = Returns item from the tuple with min value. 5) tuple(seq) = Converts a list into tuple. Built-in Dictionary Functions &amp; Methods Python includes ...