SlideShare a Scribd company logo
9
Most read
12
Most read
13
Most read
Data structures in Python
Data Structures in Python :
Lists
Dictionary
Tuples
Sets
 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.
 Example : s1=[‘abc’,’xyz’,’pqr’]
 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
 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. 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[:] .
 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.
 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.
 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 [].
 Keys could be a string in dictionary.
 We can nest dictionaries.
 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:
 Example : { ‘name’: ‘John’, ‘age’ : ‘seven’}
 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.
 Example : S1=(‘John’ , ‘Rajkot’ )
 Tuples may be nested.
 A tuple consists of a number of values separated by
commas.
 Extract positions in tuples using slices.
 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.
 Creates an empty set using
 Set membership in sets
 We can convert a list into sets.
 Example : {1,2,3,4,5}
 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”
 String is defined as a sequence or list of characters.
 String is immutable in nature means once defined , they
cannot be changed.
 str is the type for strings in python.
 Define strings using quotes (“ or ‘ or “““)
>>> st = “Hello World”
>>> st = ‘Hello World’
>>> st = “““This is a multi-line
string that uses triple quotes.”””
‘ ‘ can be used to escape quotes:
STRINGS
 The string is enclosed in double quotes, if the string
contains a single quote
and no double quotes, otherwise it is enclosed in single
quotes.
 The print() function produces a more readable output, by
omitting the enclosing quotes and by printing escaped and
special characters:
 ‘n’ is used to place a string in newline.
 Example : x=“John”
 If you don’t want characters prefaced by  to be interpreted
as special characters, you can use raw strings by adding
an r before the first quote:
 Two or more string literals (i.e. the ones enclosed between
quotes) next to each
other are automatically concatenated.
 Attempting to use an index that is too large
result in an error.
 To access substrings, use the square brackets for
slicing along with the index or indices to obtain your
substring.
Data structures in Python
 CONCATENATION (+) :
 adds value on the either sde of operator.
 REPETITION (*) :
 creates new strings,concatenating multiple copies of the same
strings.
 SLICE ([]) :
 gives the character from the given index
 RANGE SLICE ([ :]) :
 gives the character from the given range.
 MEMBERSHIP (in) or (not in) : membership returns true or
false if the character exist in the given string.
 length (len): It is used to find the length of the string.
 upper() : upper() is used to uppercase the string .
 lower() : lower() operation is used to lowercase the string.
 Cannot update a string “in place” as strings are
immutable in nature.
 Instead ,use slices and concatenation for
modification of strings:-
 A slice is a segment of string.
 Slicing allows you to obtain substring
 Syntax:


Str[start : end]
Start: substring start from this element
End: end of substring excluding the element at
this index
 S[i:j] starts at s[i] and ends at s[j-1]
 S[:j] means start at s[0], so s[0:j]
 S[i:] means ends at s[len(s)-1]
 str.format () is used to format the string.
 Examples: Replace argument by position in message string.
 Replace argument by names in message string.
Data structures in Python

More Related Content

PDF
9 python data structure-2
PPTX
Recursion in Data Structure
PPT
Data Structures- Part5 recursion
PDF
Immutable vs mutable data types in python
PPTX
Datastructures in python
PDF
What is Tuple in python? | Python Tuple Tutorial | Edureka
PDF
Python list
9 python data structure-2
Recursion in Data Structure
Data Structures- Part5 recursion
Immutable vs mutable data types in python
Datastructures in python
What is Tuple in python? | Python Tuple Tutorial | Edureka
Python list

What's hot (20)

PPTX
Loops in Python
PPT
Priority queues
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PDF
Python Programming - XI. String Manipulation and Regular Expressions
PPT
Arrays
PDF
Namespaces
PDF
Introduction to NumPy (PyData SV 2013)
PPTX
Python Data Structures and Algorithms.pptx
PPTX
Tree Traversal
PPT
Chapter 13 - Recursion
PDF
Python lists & sets
PPT
Python Dictionaries and Sets
PPTX
List in Python
PDF
Python set
PPTX
List in Python
PDF
Python-03| Data types
PPT
Arrays Data Structure
PPTX
Circular Queue data structure
PPTX
Regular expressions in Python
PPTX
Python Datatypes by SujithKumar
Loops in Python
Priority queues
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Programming - XI. String Manipulation and Regular Expressions
Arrays
Namespaces
Introduction to NumPy (PyData SV 2013)
Python Data Structures and Algorithms.pptx
Tree Traversal
Chapter 13 - Recursion
Python lists & sets
Python Dictionaries and Sets
List in Python
Python set
List in Python
Python-03| Data types
Arrays Data Structure
Circular Queue data structure
Regular expressions in Python
Python Datatypes by SujithKumar
Ad

Similar to Data structures in Python (20)

PPTX
Data Structures in Python
PPTX
dataStructuresInPython.pptx
PPTX
PDF
Python revision tour II
PDF
Processing data with Python, using standard library modules you (probably) ne...
PPT
Data types usually used in python for coding
PPT
02python.ppt
PPT
02python.ppt
PPTX
Python Workshop
PPTX
Chapter - 2.pptx
PPTX
UNIT-3 python and data structure alo.pptx
PDF
Python Basics it will teach you about data types
PPTX
PPTX
cover every basics of python with this..
PPTX
11 Introduction to lists.pptx
PDF
Data structures in python
PDF
ppt notes python language operators and data
PDF
ppt_pspp.pdf
PPTX
Python-The programming Language
PDF
Python - Lecture 3
Data Structures in Python
dataStructuresInPython.pptx
Python revision tour II
Processing data with Python, using standard library modules you (probably) ne...
Data types usually used in python for coding
02python.ppt
02python.ppt
Python Workshop
Chapter - 2.pptx
UNIT-3 python and data structure alo.pptx
Python Basics it will teach you about data types
cover every basics of python with this..
11 Introduction to lists.pptx
Data structures in python
ppt notes python language operators and data
ppt_pspp.pdf
Python-The programming Language
Python - Lecture 3
Ad

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPT
Chapter 6 Design in software Engineeing.ppt
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
web development for engineering and engineering
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Foundation to blockchain - A guide to Blockchain Tech
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Chapter 6 Design in software Engineeing.ppt
Embodied AI: Ushering in the Next Era of Intelligent Systems
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
CH1 Production IntroductoryConcepts.pptx
OOP with Java - Java Introduction (Basics)
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
web development for engineering and engineering
Lesson 3_Tessellation.pptx finite Mathematics
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd

Data structures in Python

  • 2. Data Structures in Python : Lists Dictionary Tuples Sets
  • 3.  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.  Example : s1=[‘abc’,’xyz’,’pqr’]
  • 4.  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
  • 5.  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.
  • 6.  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[:] .  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.
  • 7.  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.
  • 8.  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 [].  Keys could be a string in dictionary.
  • 9.  We can nest dictionaries.  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:  Example : { ‘name’: ‘John’, ‘age’ : ‘seven’}
  • 10.  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.  Example : S1=(‘John’ , ‘Rajkot’ )
  • 11.  Tuples may be nested.  A tuple consists of a number of values separated by commas.  Extract positions in tuples using slices.
  • 12.  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.  Creates an empty set using  Set membership in sets  We can convert a list into sets.  Example : {1,2,3,4,5}
  • 13.  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”
  • 14.  String is defined as a sequence or list of characters.  String is immutable in nature means once defined , they cannot be changed.  str is the type for strings in python.  Define strings using quotes (“ or ‘ or “““) >>> st = “Hello World” >>> st = ‘Hello World’ >>> st = “““This is a multi-line string that uses triple quotes.””” ‘ ‘ can be used to escape quotes:
  • 15. STRINGS  The string is enclosed in double quotes, if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes.  The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:  ‘n’ is used to place a string in newline.  Example : x=“John”
  • 16.  If you don’t want characters prefaced by to be interpreted as special characters, you can use raw strings by adding an r before the first quote:  Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.  Attempting to use an index that is too large result in an error.
  • 17.  To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.
  • 19.  CONCATENATION (+) :  adds value on the either sde of operator.  REPETITION (*) :  creates new strings,concatenating multiple copies of the same strings.  SLICE ([]) :  gives the character from the given index  RANGE SLICE ([ :]) :  gives the character from the given range.
  • 20.  MEMBERSHIP (in) or (not in) : membership returns true or false if the character exist in the given string.  length (len): It is used to find the length of the string.  upper() : upper() is used to uppercase the string .  lower() : lower() operation is used to lowercase the string.
  • 21.  Cannot update a string “in place” as strings are immutable in nature.  Instead ,use slices and concatenation for modification of strings:-
  • 22.  A slice is a segment of string.  Slicing allows you to obtain substring  Syntax:   Str[start : end] Start: substring start from this element End: end of substring excluding the element at this index  S[i:j] starts at s[i] and ends at s[j-1]  S[:j] means start at s[0], so s[0:j]  S[i:] means ends at s[len(s)-1]
  • 23.  str.format () is used to format the string.  Examples: Replace argument by position in message string.  Replace argument by names in message string.