0% found this document useful (0 votes)
20 views50 pages

Lists

Uploaded by

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

Lists

Uploaded by

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

Python List

Python Collections 
There are four collection data types in the Python programming language:
• List:
is a collection which is ordered and changeable. Allows duplicate members.
• Tuple: 
is a collection which is ordered and unchangeable. Allows duplicate members.
• Set: 
is a collection which is unordered and unindexed. No duplicate members
• Dictionary :
is a collection which is unordered, changeable and indexed. No duplicate
members.
Python List
• Lists are just like the arrays, declared in other languages.
• 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 starts from “0”.
• Lists are a useful tool for preserving a sequence of data and further iterating
over it
Creating a List
• A list can be defined as a collection of values or items of different types.
The items in the list are separated with the comma (,) and enclosed with
the square brackets [].
OR
• Lists in Python can be created by just placing the sequence inside the
square brackets[].
Syntax : list_name = [list_element1, list_element2, list_element3,……]
Creating a List
Python list() Function
BY using list() function we can create lists in python.
Syntax: list(iterable)  

iterable (optional) - An object that can be a sequence( string, tuple etc.) or


collection( set, dictionary etc.) or iterator object.
Accessing elements of a list
• We can access the list elements in the same way as it happens with the
strings.
• The elements of the list can be accessed by using the index operator [].
Syntax : Name_of_variable [Index value]
The first element of the list is stored at the 0th index, the second element
of the list is stored at the 1st index, and so on.
negative indexing
• Python provides us the flexibility to use the negative indexing also.
• The negative indices are counted from the right. 
• The last element (right most) of the list has the index -1, its adjacent
left element is present at the index -2 and so on until the left most
element is encountered.
LIST SLICING [START : END]
• The slicing operator returns a subset of a list , called slice by
specifying two indices , i.e. start and end.
Syntax: name_of_the_variable[start index : end index]
LIST SLICING [START : END: STEP SIZE]
PYHTON BUILT-IN FUNCTIONS FOR
LIST
• Python provides various built-in functions which can be used with the
lists.
1. len()
The len() function returns the number of items (length) in a list
PYHTON BUILT-IN FUNCTIONS FOR LIST
max() function
Calculates maximum of all the elements of List
Syntax: max(List)
PYHTON BUILT-IN FUNCTIONS FOR
LIST
• min() function
Calculates minimum of all the elements of List.
Syntax: min(List)
PYHTON BUILT-IN FUNCTIONS FOR LIST
sum() function
Calculates sum of all the elements of List.
Syntax: sum(List)
Note: Sum is calculated only for Numeric values, elsewise throws TypeError.
PYHTON BUILT-IN FUNCTIONS FOR LIST

random.shuffle() function
Shuffle a list (reorganize the order of the list items) items.
The shuffle() method takes a sequence (list, string, or tuple) and
reorganize the order of the items.
The List operators
“+” Operator
The concatenation operator + is used to join the list elements.
The List operators
“*” Operator
The multiplication operator + is used to replicate the elements in the
list.
The List operators
in operator
• The ‘in’ operator is used to check if a value exists in the list or not.
• Evaluates to true if the element present in the list and false otherwise.
The List operators
• “is” Operator
Let us execute the following two statements:
A='Python'
B='Python'
We know that both A and B refer to a string but we don't know whether
they refer to the same string or else.
There are two possible situations as follows.
A → 'Python’ A ‘Python’
B → 'Python’ Or B

In the first case, A and B refer to two different objects that have same
values. In second case, they refer to the same object.
• To understand whether two variables refer to the same object, we use the 'is'
operator.

• In case of string , if both the variables contain the same values , then both of then
refer to the same object.
• In case of lists , if both the variables contain the same values , then both of then
refer to different object.
The List operators

• The del Operator


del list_name[a : b] :- This method deletes all the elements in
range starting from index ‘a’ till ‘b’ mentioned in
arguments.
Mutability – List aliasing and list cloning
• The list are mutable but strings are not mutable.
• mutable means that where we can change the content of the list by
accessing directly.
List Aliasing
• We know that the variables refer to objects, if we assign one variable
to another, both variables refer to the same object

• In the above the same list has two different names, a and b, we say that
it is aliased. Changes made with one alias affect the other.
Cloning list
• If we wants to modify existing list and also wants to keep the original
one, the we make a copy of the list, The process of copying list called
cloning.
• The slice(:) operator is used to clone the list.
• If cloning has been done , the changes made in one list will not effect
to the original existing list
List Comprehensions
• The list comprehension is used to create a new list from existing
sequences.
• It is a tool for transforming a given list into another list.
• List comprehensions provide a concise way to create lists.
• List Comprehension is defined as an elegant way to define, create a
list in Python and consists of brackets that contains an expression
followed by for clause.
Syntax : [ expression for item in list if conditional ]
• It is efficient in both computationally and in terms of coding space
and time.
Implement the following mathematical
expressions using list comprehensions
A = {X2: x in {O.........9}}
B = {X3: x in {O......9}}
C = {X : x in A and even}
Write a program to create a list 'A' to generate squares of a number
(from 1 to 10), list 'B' to generate cubes of a number (from 1 to 10) and
list 'C' with those element that are even and present in list ‘A’.
Consider the list with five different Celsius values. Convert all the Celsius values into Fahrenheit
( Formulae to convert Celsius Values to Fahrenheit.
Fahrenheit = (9/5) * Celsius + 32)
Consider the list with mixed type of elements, such as L1 = [1, 'X', 4,5.6, ‘z', 9, 'a', 0, 41] . Create another
list using list comprehension which consists of only the integer's present in LI.
List methods
• Once a list is created, we can use the methods of list class to
manipulate the list.
append(x) : Adds an element x to the end of the list.
List methods

clear() : Removes all elements in the list.


List methods

• count (X) : Returns the number of times element x appears in


the list.
List methods
• Copy () : returns a copy of the list
List methods

• extend() : Append all the elements of one list into another


List methods
• index(x) : Return the index of first occurrence of element x from the
list
List methods
• insert(int index, object x ) : Insert the element at given index
List methods
• pop(i) : Removes the element from the given position
List methods
• remove(object x) : Removes the first occurrence of element x
List methods
• reverse() : Reverse the elements of the list
List methods
• sort() : sort the elements of the list
Conversion of string to list
• A string is a sequence of characters and list is a sequence of values.
• But a list of characters is not the same as string.
• List() function is used to convert string into list values
Splitting a string into list values
• By using split() method we can split the string into words
Passing list to a function
• We can pass a list to a function and perform various operations on the
list, even we may change the content of the list after passing a list to
the function.
Returning list from a function
• A function can return a list.

You might also like