Lists
Lists
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)
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
• 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