0% found this document useful (0 votes)
34 views22 pages

Lists and Dictionaries

This document discusses lists and dictionaries in Python. It provides information on: - Lists allow storing sequences of data of any type, while dictionaries organize data by association rather than position. - Common list operations include accessing elements by index, concatenation, length checking, and membership testing. Lists support one and multi-dimensional storage. - Dictionaries associate keys with values, where keys can be any immutable type like strings or integers. Values can be any type. - Keys are added or replaced using subscript notation, checked for existence using get() or in, and removed using pop(). Dictionaries can be iterated over using a for loop or the items() method.

Uploaded by

ALan d'ToDz CRew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views22 pages

Lists and Dictionaries

This document discusses lists and dictionaries in Python. It provides information on: - Lists allow storing sequences of data of any type, while dictionaries organize data by association rather than position. - Common list operations include accessing elements by index, concatenation, length checking, and membership testing. Lists support one and multi-dimensional storage. - Dictionaries associate keys with values, where keys can be any immutable type like strings or integers. Values can be any type. - Keys are added or replaced using subscript notation, checked for existence using get() or in, and removed using pop(). Dictionaries can be iterated over using a for loop or the items() method.

Uploaded by

ALan d'ToDz CRew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 22

Lists and Dictionaries

in
Python

What are the Function of List and


Dictionary

A list allows the programmer to manipulate a


sequence of data values of any types
A dictionary organizes data values by
association with other data values rather than
by sequential position.

Lists

A list is a sequence of data values called


items or elements.

An item can be of any type.

Here are some real-world examples of lists:

A shopping list for the grocery store, a to do


list, a roster for an athletic team, A guest list
for a wedding, A recipe, which is a list of
instructions, etc

Lists

One dimension List

Multi dimension List

Lists

Range function can be used to generate an


integer list
The function len and the subscript operator []
work just as they do for strings:

Lists

Concatenation and equality operator can be


used in lists
In operator can be used in list too

Operator and function in list

List Methods for Inserting and


Removing Elements

Aliasing and Side Effect

Aliasing is two objects that refer to each other


Side Effect is when a value in one of the list
of two object that mutual refer change, then
automatically a value in the other object will
change and have the same value as the
other object

Equality: Object Identity and


Structural Equivalence

Object identity is if the two objects has alias


for the same object
Structural Equivalence is if the two different
object have the same contents
Pythons is operator can be used to test for
object identity. It returns True if the two
operands refer to the exact same object, and
it returns False if the operands refer to
distinct objects (even if they are structurally
equivalent)

Equality: Object Identity and


Structural Equivalence

Tuple

Use a pair of () to declare a tuple

Dictionary

A dictionary organizes information by


association, not position.
In computer science, data structures
organized by association are also called
tables or association lists.
In Python, a dictionary associates a set of
keys with data values.

Dictionary

The keys in a dictionary can be data of any


immutable types, including other data
structures, although keys normally are strings
or integers.
The associated values can be of any types.

Adding Keys and Replacing Values


in Dictionary

You add a new key/value pair to a dictionary


by using the subscript operator [] .The form of
this operation is the following:
Example :

To Check Key in Dictionary

Use get method to check if the key is exist in the


dictionary
This method expects two arguments, a possible key
and a default value.
If the key is in the dictionary, the associated value is
returned. If the otherwise happens, the default value
is returned
Example :

info[name] = Sandy

info[occupation] = hacker

print info.get(job,None)

Remove Key in Dictionary

Pop is used to remove the key in dictionary


This method expects a key and an optional default
value as arguments
If the key is in the dictionary, it is removed and its
associated value is returned. Otherwise, the default
value is returned
Example :

info[name] = Sandy

info[occupation] = hacker

print info.pop(job,None)

print info.pop(occupation)

Use the for loop for printing items in


Dictionary

Example to print the items in info dictionary

You can also use dictionary method items to print


the content of the dictionary

Dictionary Operation

Dictionary Operation

Exercise

Write dictionary data structure write the application


to convert hexadecimal and octal into binary
number system

Reference
Fundamentals of Python from First Program
Through Data Structures Chapter 5

You might also like