0% found this document useful (0 votes)
5 views104 pages

05 - Data Structures

The document provides an overview of lists in Python, highlighting their dynamic nature, mutability, and ability to contain heterogeneous data types. It explains how to create, access, slice, add, and remove elements from lists, as well as introduces lambda functions for creating anonymous functions. Additionally, it discusses the advantages of generators and iterators in Python for efficient data handling.

Uploaded by

rizqi.aiml01
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)
5 views104 pages

05 - Data Structures

The document provides an overview of lists in Python, highlighting their dynamic nature, mutability, and ability to contain heterogeneous data types. It explains how to create, access, slice, add, and remove elements from lists, as well as introduces lambda functions for creating anonymous functions. Additionally, it discusses the advantages of generators and iterators in Python for efficient data handling.

Uploaded by

rizqi.aiml01
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/ 104

05

Data Structures
Lists
• Lists are just like dynamic sized arrays.
• Lists need not be homogeneous always which makes it a most powerful
tool in Python.
• A single list may contain DataTypes like Integers, Strings, as well as Objects.
• Lists are mutable, and hence, they can be altered even after their creation.
• List in Python are ordered and have a definite count.
• The elements in a list are indexed according to a definite sequence and the
indexing of a list is done with 0 being the first index.
• Each element in the list has its definite place in the list, which allows
duplicating of elements in the list, with each element having its own
distinct place and credibility.
Creating a List
• Lists in Python can be created by just placing the sequence inside the
square brackets[]
• List may contain mutable elements
• A list may contain duplicate values with their distinct positions and
hence, multiple distinct or duplicate values can be passed as a
sequence at the time of list creation.
Accessing elements from the List
• In order to access the list items refer to the index number.
• Use the index operator [ ] to access an item in a list.
• The index must be an integer.
• Nested list are accessed using nested indexing.
• In Python, negative sequence indexes represent positions from the
end of the array. Instead of having to compute the offset as in
List[len(List)-3], it is enough to just write List[-3].
• Negative indexing means beginning from the end, -1 refers to the last
item, -2 refers to the second-last item, etc.
Slicing of a List
• In Python List, there are multiple ways to print the whole List with all
the elements, but to print a specific range of elements from the list,
we use Slice operation.
• Slice operation is performed on Lists with the use of a colon(:).
• To print elements from beginning to a range use [: Index], to print
elements from end-use [:-Index], to print elements from specific
Index till the end use [Index:], to print elements within a range, use
[Start Index:End Index] and to print the whole List with the use of
slicing operation, use [:].
• Further, to print the whole List in reverse order, use [::-1].
• Note – To print elements of List from rear end, use Negative Indexes.
Slicing of a List
Adding Elements to a List: append()
• Elements can be added to the List by using built-in append() function.
• Only one element at a time can be added to the list by using append()
method, for addition of multiple elements with the append() method,
loops are used.
• Tuples can also be added to the List with the use of append method
because tuples are immutable.
• Unlike Sets, Lists can also be added to the existing list with the use of
append() method.
Adding Elements to a List: insert()
• append() method only works for addition of elements at the end of
the List, for addition of element at the desired position, insert()
method is used.
• Unlike append() which takes only one argument, insert() method
requires two arguments(position, value).
Adding Elements to a List: extend()
• Other than append() and insert() methods, there’s one more method
for Addition of elements, extend(), this method is used to add
multiple elements at the same time at the end of the list.
Removing Elements from the List: remove()
• Elements can be removed from the List by using built-in remove()
function but an Error arises if element doesn’t exist in the set.
• Remove() method only removes one element at a time, to remove
range of elements, iterator is used.
• The remove() method removes the specified item.
• Note – Remove method in List will only remove the first occurrence of
the searched element.
Removing Elements from the List: pop()
• Pop() function can also be used to remove and return an element
from the set, but by default it removes only the last element of the
set, to remove element from a specific position of the List, index of
the element is passed as an argument to the pop() method.
Python lambda
• In Python, anonymous function means that a function is without a name.
• As we already know that def keyword is used to define the normal
functions and the lambda keyword is used to create anonymous functions.
• It has the following syntax: lambda arguments : expression
• This function can have any number of arguments but only one expression,
which is evaluated and returned.
• One is free to use lambda functions wherever function objects are
required.
• You need to keep in your knowledge that lambda functions are syntactically
restricted to a single expression.
• It has various uses in particular fields of programming besides other types
of expressions in functions.
Python lambda
• Let’s look at this example and try to understand the difference
between a normal def defined function and lambda function.
• This is a program that returns the cube of a given value:
Python lambda
• Without using Lambda : Here, both of them returns the cube of a
given number. But, while using def, we needed to define a function
with a name cube and needed to pass a value to it. After execution,
we also needed to return the result from where the function was
called using the return keyword.
• Using Lambda : Lambda definition does not include a “return”
statement, it always contains an expression which is returned. We can
also put a lambda definition anywhere a function is expected, and we
don’t have to assign it to a variable at all. This is the simplicity of
lambda functions.
In summary…
• Generators have been an important part of Python ever since they
were introduced with PEP 255.
• Generators allow you to create iterators in a very pythonic manner.
• Iterators allow lazy evaluation, only generating the next element of an
iterable object when requested. This is useful for very large data sets.
• Iterators and generators can only be iterated over once.
• Generator Functions are better than Iterators.
• Generator Expressions are better than Iterators (for simple cases
only).

You might also like