
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialList and Tuple are built-in container types defined in Python. Objects of both these types can store different other objects that are accessible by index. List as well as tuple is a sequence data type, just as string. List as well as tuple can store objects which need not be of same type.
List : A List is an ordered collection of items (which may be of same or different types) separated by comma and enclosed in square brackets.
In [1]:
L1=[10,25.5,3+2j,"Hello"]
L1
Out[1]:
[10, 25.5, (3+2j), 'Hello']
In above list, each item is of different type. Further, each item is accessible by positional index starting from 0. Hence L1[2] will return 25.5
In [2]:
L1[1]
Out[2]:
25.5
Tuple: Tuple looks similar to list. The only difference is that comma separated items of same or different type are enclosed in parentheses. Individual items follow zero based index, as in list or string.
In [3]:
T1=(10,25.5,3+2j,"Hello")
T1
Out[3]:
(10, 25.5, (3+2j), 'Hello')
In [4]:
T1[1]
Out[4]:
25.5
The obvious difference is the use of square brackets [] in List and parentheses () in tuple as enclosures. However, the important difference is that List as a mutable object and Tuple is an immutable object.
If contents of an object can be modified in place, after it has been instantiated, is a mutable object. On the other hand, any operation on immutable object that tries to modify its contents is prohibited.
In above example, any item in the list L1 can be assigned with different value. Let us change value of item at index=2 from 3+2j to 1.22E-5
In [5]:
L1[2]=1.22E-5
L1
Out[5]:
[10, 25.5, 1.22e-05, 'Hello']
The built-in List class has different methods that allow various operations on List object (such as insertion, deletion, sorting etc)
However, any such operation is not possible with Tuple object. If we try to modify T1 by changing value of item at index=2 to 1.22E-5, TypeError exception is raised.
In [6]:
T1[2]=1.22E-5
T1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-18a8ef5d3740> in <module>()
----> 1 T1[2]=1.22E-5
2 T1
TypeError: 'tuple' object does not support item assignment
Following built-in functions can be used along with List as well as Tuple.
len() | Returns number of elements in list/tuple |
max() | If list/tuple contains numbers, largest number will be returned. If list/tuple contains strings, one that comes last in alphabetical order will be returned. |
min() | If list/tuple contains numbers, smallest number will be returned. If list/tuple contains strings, one that comes first in alphabetical order will be returned. |
sum() | Returns addition of all elements in list/tuple |
sorted() | sorts the elements in list/tuple |
In [7]:
L1=[10,30,50,20,40]
T1=(10,50,30,40,20)
print (len(L1))
print (len(T1))
print ('max of L1', max(L1))
print ('max of T1', max(T1))
print ('min of L1', min(L1))
print ('min of T1', min(T1))
print ('sum of L1', sum(L1))
print ('sum of T1', sum(T1))
print ('L1 in sorted order', sorted(L1))
print ('T1 in sorted order', sorted(T1))
Out[7]:
5
5
max of L1 50
max of T1 50
min of L1 10
min of T1 10
sum of L1 150
sum of T1 150
L1 in sorted order [10, 20, 30, 40, 50]
T1 in sorted order [10, 20, 30, 40, 50]
If items in list/tuple are strings, min() and max() functions returns string that comes first/last in alphabetical order. If list/tuple is made up of numeric and nonnumeric values, TypeError exception is raised as comparison of dissimilar objects is not possible.
In [8]:
L2=['pen', 'book','computer', 'table', 'file']
T2=('pen', 'book','computer', 'table', 'file')
print ('max of L2', max(L2))
print ('max of T2', max(T2))
print ('min of L2', min(L2))
print ('min of T2', min(T2))
max of L2 table
max of T2 table
min of L2 book
min of T2 book
Out [9]:
L3=[100, "hundred", 0.001]
print ('max of L3', max(L3))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-9ea5851a5ca9> in <module>()
1 L3=[100, "hundred", 0.001]
----> 2 print ('max of L3', max(L3))
TypeError: '>' not supported between instances of 'str' and 'int'
The built-in list class has following methods to perform various operations on list object. Following methods allow new items to be added to list.
append() | appends an object to end of list |
copy() | makes a shallow copy of list |
count() | return number of occurrences of value in list |
extend() | extends the list by appending elements from another list/tuple |
insert() | inserts object in the list before given index |
In [10]:
L1=[10,30,50,20,40]
L1.append(100) #appends new item
print ("after append",L1)
L1.insert(2,30) #inserts new value at index
print ('after insert',L1)
c=L1.count(30)
print ('count of 30',c)
L1.extend([11,22,33])
print ('after extend', L1)
Out [10]:
after append [10, 30, 50, 20, 40, 100]
after insert [10, 30, 30, 50, 20, 40, 100]
count of 30 2
after extend [10, 30, 30, 50, 20, 40, 100, 11, 22, 33]
Following methods are used to remove items from given list.
pop() | removes and returns item at given index . Raises IndexError if list is empty or index is out of range. |
remove() | removes first occurrence of value in the list. Raises ValueError if the value is not present. |
clear() | remove all items from the list |
In [11]:
p=L1.pop()
print ('item popped:', p)
print ('list after popping', L1)
L1.remove(100)
print ('after removing value :',L1)
L1.clear()
print ('all cleared:', L1)
Out [11]:
item popped: 33
list after popping [10, 30, 30, 50, 20, 40, 100, 11, 22]
after removing value : [10, 30, 30, 50, 20, 40, 11, 22]
all cleared: []
Following methods rearrange sequence of items in the list
reverse() | reverses the list in place |
sort() | sorts the list in place |
In [12]:
L1=[10, 30, 30, 50, 20, 40, 11, 22]
print ('original list :', L1)
L1.reverse()
print ('after reversing:',L1)
L1.sort()
print ("sorted list: ", L1)
Out [12]:
original list : [10, 30, 30, 50, 20, 40, 11, 22]
after reversing: [22, 11, 40, 20, 50, 30, 30, 10]
sorted list: [10, 11, 20, 22, 30, 30, 40, 50]
If you recall, tuple is an immutable object. Hence the tuple class doesn’t have similar methods perform insertion, deletion or rearrangement of items.
All sequence type objects (string, list and tuple) are inter-convertible. Python’s built-in functions for this purpose are explained below:
list() | converts a tuple or string to list |
tuple() | converts list or string to tuple |
str() | returns string representation of list or tuple object |
In [13]:
L1=[10, 30, 30, 50, 20, 40, 11, 22]
T1=tuple(L1)
print (T1)
(10, 30, 30, 50, 20, 40, 11, 22)
In [14]:
T1=(10,50,30,40,20)
L1=list(T1)
print (L1)
[10, 50, 30, 40, 20]
In [15]:
s1="Hello"
L2=list(s1)
print ('string to list:', L2)
T2=tuple(s1)
print ('string to tuple', T2)
string to list: ['H', 'e', 'l', 'l', 'o']
string to tuple ('H', 'e', 'l', 'l', 'o')
In [16]:
s1=str(L1)
s2=str(T1)
print ('list to string',s1)
print ('tuple to string',s2)
list to string [10, 50, 30, 40, 20]
tuple to string (10, 50, 30, 40, 20)
When a list or tuple is converted to string by str() function, the string representation is not exactly similar to word, but list or tuple surrounded by single quote marks. To form a continuous sequence of characters in the list, use join() method of string object.
In [17]:
L2=['H', 'e', 'l', 'l', 'o']
s1=str(L2)
s1
Out[17]:
"['H', 'e', 'l', 'l', 'o']"
In [18]:
s2="".join(L2)
print ('string from list items:', s2)
string from list items: Hello
In this chapter, we discussed the list and tuple objects, their functions and methods. In next chapter we shall learn about dictionary data type.