0% found this document useful (0 votes)
29 views

Data Structures in Python

Uploaded by

deepgyan780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Data Structures in Python

Uploaded by

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

DATA STRUCTURES IN PYTHON

Data Structures in Python :

 Lists

 Dictionary

 Tuples

 Sets
LISTS
 List is most versatile datatype available in Python, written as a list of comma-
separated values (items) between square brackets.
 Items in a list can be Heterogenous types(need not be of the same type).
 Lists are mutable
 Concatenation produces a new lists.

 Append function extends a list with a new value without changing it.
LISTS ARE MUTABLE
 A mutable object can be changed after it's created, and an immutable object
cannot be changed after its created.

 Examples of mutable objects are dictionary,lists etc

 Example of lists in mutable form is:


LIST FUNCTIONS
 list. append ( x) :Add an item to the end of the list.

 list. extend ( L): Extend the list by appending all the items in the given list.

 list. insert ( i, x): Insert an item at a given position.

 list. remove ( x) : Remove the first item


from the list whose value is x.
It gives an error if there is no such item.
LIST FUNCTIONS
 list. pop ( [i]): Remove the item at the given position in the list, and return it.

 list. clear ( ): Remove all items from the list. Equivalent to del a[:] .
Not supported in python 2.X.
 list.reverse(): Reverse a given list.
 list. index ( x): Return the index in the list of the first item whose value is x. It is an
error if there is no such item.

 list. count ( x): Return the number of times x appears in the list.

 list. sort ( ): Sort the items of the list in place.


USING LISTS AS STACKS
 We can also use lists as a stack in Python.

 Stack follows a property of last in first out(LIFO Rule).

 To add an item to the top of the stack, use append() .

 To retrieve an item from the top of the stack, use pop() without an explicit index.

 Example:
USING LISTS AS QUEUES
 Python also uses a list as a queue where the first element added is the first element
retrieved.
 Lists are not efficient when used as a queue.
 While appends and pops from the end of list are fast, doing inserts or pops from the
beginning of a list is slow.
 To implement a queue, use collections.deque which was designed to have fast
appends and pops from both ends.
 Example:
List Comprehensions
 List comprehensions is very similar to set theory or set builder form .
 List comprehensions provide a concise way to create a lists.
 Allows to build a new set from existing sets.
 Is an extension to the lists.
 Examples:
 Create a list of two tuples using list comprehension

 Flattens a list using a list comprehension using “for”.


DICTIONARY
 Dictionary is defined as an unordered set of key: value pairs, with the
requirement that the keys are unique . \
 Dictionary is a Mutable datatype.
 Dictionaries are sometimes found in other languages as “associative memories”
or “associative arrays”.
 Dictionaries are indexed by keys, which can be any immutable type or could be
a string.
 A pair of braces creates an empty dictionary: {} , not [].
 INITIALISATION: example be

 Keys could be a string in dictionary. Example


DICTIONARY
 We can nest dictionaries. Example:

 Directly assign values to a dictionary.

 Dictionary comprehensions can be used to create dictionaries from arbitrary key


and value expressions:

 The dict() constructor builds dictionaries directly from sequences of key-value


pairs:
TUPLES
 Tuple is a sequence data type.
 Tuples are immutable, and usually contain a heterogeneous sequence of
elements.

 Simultaneous assignment is possible in tuples.

 In tuple, we can assign a tuple of value to a name.


TUPLES
 Tuples may be nested.

 A tuple consists of a number of values separated by commas.

 Extract positions in tuples using slices.


SETS
 A set is an unordered collection with no duplicate elements.
 Set objects also support mathematical operations like union, intersection,
difference, and symmetric difference.
 Curly braces or the set() function can be used to create sets.
 Example:

 Creates an empty set using


 Set membership in sets

 We can convert a list into sets.


SETS OPERATIONS
 Union : union of two sets is done using “set1|set2”

 Intersection : intersection of two sets can be done using “set1&set2”

 Set difference: In this, elements present in set2 is not included in the resultant set.
It can be don using “set1-set2

 Exclusive or: The syntax for exclusive or is “set1^set2”


•Thankyou

You might also like