SlideShare a Scribd company logo
List & Tuple Data in Python
Prof. K. Adisesha| 1
Python Lists
Python Collections (Arrays)
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.
When choosing a collection type, it is useful to understand the properties of that type. Choosing
the right type for a particular data set could mean retention of meaning, and, it could mean an
increase in efficiency or security.
List: A list is a collection, which is ordered and changeable. In Python, lists are written with
square brackets.
Example
Create a List:
StuList = ["Prajwal", "Sunny", "Rekha"]
print(StuList)
Output: ['Prajwal', 'Sunny', 'Rekha']
Access Items: You access the list items by referring to the index number:
Example
Print the second item of the list:
StuList = ["Prajwal", "Sunny", "Rekha"]
print(StuList[1])
Output: Sunny
Negative Indexing: Negative indexing means beginning from the end, -1 refers to the last item,
-2 refers to the second last item etc.
Example
Print the last item of the list:
StuList = ["Prajwal", "Sunny", "Rekha"]
print(StuList[-1])
Output: Rekha
Range of Indexes: You can specify a range of indexes by specifying start and end position of the
range.
 When specifying a range, the return value will be a new list with the specified items.
Example
Return the third, fourth, and fifth item:
StuList = ["Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu"]
print(StuList[2:5])
List & Tuple Data in Python
Prof. K. Adisesha| 2
Output: ["Rekha", "Sam", "Adi"]
Note: The search will start at index 2 (included) and end at index 5 (not included).
Range of Negative Indexes: Specify negative indexes if you want to start the search from the end
of the list:
Example
This example returns the items from index -4 (included) to index -1 (excluded)
StuList = ["Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu"]
print(StuList[-4:-1])
Output: ["Sam", "Adi", "Ram"]
Change Item Value: To change the value of a specific item, refer to the index number:
Example
Change the second item:
StuList = ["Prajwal", "Sunny", "Rekha"]
StuList[1] = "Adi"
print(StuList)
Output: ["Prajwal", "Adi", "Rekha"]
Loop through a List: You can loop through the list items by using a for loop:
Example
Print all items in the list, one by one:
StuList = ["Prajwal", "Sunny", "Rekha"]
for x in StuList:
print(x)
Output: Prajwal
Sunny
Rekha
Check if Item Exists: To determine if a specified item is present in a list use the in keyword:
Example
Check if “Sunny” is present in the list:
StuList = ["Prajwal", "Sunny", "Rekha"]
if "Prajwal" in StuList:
print("Yes, 'Sunny' is in the Student list")
Output: Yes, 'Sunny' is in the Student list
List Length
len() method: To determine how many items a list has use the:
Example
Print the number of items in the list:
StuList = ["Prajwal", "Sunny", "Rekha"]
print(len(StuList))
Output: 3
List & Tuple Data in Python
Prof. K. Adisesha| 3
Add Items: To add an item to the end of the list, use the append), insert(), method:
 Using the append() method to append an item:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
StuList.append("Sam")
print(StuList)
Output: ['Prajwal', 'Sunny', 'Rekha', 'Sam']
 To add an item at the specified index, use the insert() method:
Example
Insert an item as the second position:
StuList = ["Prajwal", "Sunny", "Rekha"]
StuList.insert(1, "Sam")
print(StuList)
Output: ['Prajwal', 'Sam', 'Sunny', 'Rekha']
Remove Item: There are several methods to remove items from a list using: remove(), pop(), del,
clear()
 The remove() method removes the specified item:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
StuList.remove("Sunny")
print(StuList)
Output: ['Prajwal', 'Rekha']
 The pop() method removes the specified index, (or the last item if index is not specified):
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
StuList.pop()
print(StuList)
Output: ['Prajwal', 'Sunny']
 The del keyword removes the specified index:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
del StuList[0]
print(StuList)
Output: ['Sunny', 'Rekha']
 The del keyword can also delete the list completely:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
del StuList
List & Tuple Data in Python
Prof. K. Adisesha| 4
 The clear() method empties the list:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
StuList.clear()
print(StuList)
Copy a List: You cannot copy a list simply by typing list2 = list1, because: list2 will only be a
reference to list1, and changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one-way is to use the built-in copy(), list() method.
 copy() method: Make a copy of a list:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
mylist = StuList.copy()
print(mylist)
Output: ['Prajwal', 'Sunny', 'Rekha']
 list() method: Make a copy of a list:
Example
StuList = ["Prajwal", "Sunny", "Rekha"]
mylist = list(StuList)
print(mylist)
Output: ['Prajwal', 'Sunny', 'Rekha']
Join Two Lists: There are several ways to join, or concatenate, two or more lists in Python.
 One of the easiest ways are by using the + operator.
Example
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Output: ['a', 'b', 'c', 1, 2, 3]
Another way to join two lists are by appending all the items from list2 into list1, one by one:
Example
Append list2 into list1:
list1 = ["a", "b”, "c"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
Output: ['a', 'b', 'c', 1, 2, 3]
 Use the extend() method, which purpose is to add elements from one list to another list:
Example
List & Tuple Data in Python
Prof. K. Adisesha| 5
Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b”, "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Output: ['a', 'b', 'c', 1, 2, 3]
The list() Constructor: It is also possible to use the list() constructor to make a new list.
Example
Using the list() constructor to make a List:
StuList = list(("Prajwal", "Sunny", "Rekha")) # note the double round-brackets
print(StuList)
Output: ['Prajwal', 'Sunny', 'Rekha']
List Methods
Python has a set of built-in methods that you can use on lists.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
List & Tuple Data in Python
Prof. K. Adisesha| 6
Python Tuples
A tuple is a collection, which is ordered and unchangeable. In Python, tuples are written with
round brackets.
Example
Create a Tuple:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
print(Stu_tuple)
Output: ('Prajwal', 'Sunny', 'Rekha')
Access Tuple Items: Tuple items access by referring to the index number, inside square
brackets:
Example
Print the second item in the tuple:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
print(Stu_tuple[1])
Output: Sunny
Negative Indexing: Negative indexing means beginning from the end, -1 refers to the last item,
and -2 refers to the second last item etc.
Example
Print the last item of the tuple:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
print(Stu_tuple[-1])
Output: Rekha
Range of Indexes: You can specify a range of indexes by specifying start and end of the range.
When specifying a range, the return value will be a new tuple with the specified items.
Example
Return the third, fourth, and fifth item:
Stu_tuple = ("Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu")
print(Stu_tuple[2:5])
Output: (“Rekha", "Sam", "Adi”)
Note: The search will start at index 2 (included) and end at index 5 (not included).
Range of Negative Indexes: Specify negative indexes if you want to start the search from the end
of the tuple:
Example
This example returns the items from index -4 (included) to index -1 (excluded)
Stu_tuple = ("Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu")
print(Stu_tuple[-4:-1])
Output: ("Sam", "Adi", "Ram")
List & Tuple Data in Python
Prof. K. Adisesha| 7
Change Tuple Values: Once a tuple is created, you cannot change its values. Tuples are
unchangeable or immutable as it also is called. However, by converting the tuple into a list, change
the list, and convert the list back into a tuple.
Example
Convert the tuple into a list to be able to change it:
x = (“Prajwal”, “Sunny”, “Rekha”)
y = list(x)
y[1] = "Adi"
x = tuple(y)
print(x)
Output: (“Prajwal”, “Adi”, “Rekha”)
Loop through a Tuple: You can loop through the tuple items by using a for loop.
Example
Iterate through the items and print the values:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
for x in Stu_tuple:
print(x,”t”)
Output: Prajwal Sunny Rekha
Check if Item Exists: To determine if a specified item is present in a tuple use the in keyword:
Example
Check if "Prajwal" is present in the tuple:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
if "Prajwal" in Stu_tuple:
print("Yes, 'Prajwal' is in the Student tuple")
Output: Yes, 'Prajwal' is in the Student tuple
Tuple Length: To determine how many items a tuple has, use the len() method:
Example
Print the number of items in the tuple:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
print(len(Stu_tuple))
Output: 3
Adding Items: Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
Example
You cannot add items to a tuple:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
Stu_tuple[3] = "Adi" # This will raise an error
print(Stu_tuple) Output: TypeError: 'tuple' object does not support item assignment
Create Tuple With One Item: To create a tuple with only one item, you have add a comma
after the item, unless Python will not recognize the variable as a tuple.
List & Tuple Data in Python
Prof. K. Adisesha| 8
Example
One item tuple, remember the comma:
Stu_tuple = ("Prajwal",)
print(type(Stu_tuple))
Stu_tuple = ("Prajwal") #NOT a tuple
print(type(Stu_tuple))
Output: <class 'tuple'>
<class 'str'>
Remove Items: Tuples are unchangeable, so you cannot remove items from it, but you can delete
the tuple completely:
Example
The del keyword can delete the tuple completely:
Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”)
del Stu_tuple
print(Stu_tuple) #this will raise an error because the tuple no longer exists
Join Two Tuples: To join two or more tuples you can use the + operator:
Example
Join two tuples:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
Output: ('a', 'b', 'c', 1, 2, 3)
The tuple() Constructor: It is also possible to use the tuple() constructor to make a tuple.
Example
Using the tuple() method to make a tuple:
Stu_tuple = tuple((“Prajwal”, “Sunny”, “Rekha”)) # note the double round-brackets
print(Stu_tuple)
Output: ('Prajwal', 'Sunny', 'Rekha')
Tuple Methods
Python has two built-in methods that you can use on tuples.
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it
was found

More Related Content

PDF
Set methods in python
PDF
Python set
PPT
Stacks & Queues
PDF
Python list
PPTX
Python strings presentation
PPTX
Packages In Python Tutorial
PPT
Queue Data Structure
Set methods in python
Python set
Stacks & Queues
Python list
Python strings presentation
Packages In Python Tutorial
Queue Data Structure

What's hot (20)

PDF
Python strings
PPT
Lec 17 heap data structure
PPTX
Data structure - Graph
PDF
Python programming : Strings
PPT
Queue data structure
PPTX
Python Libraries and Modules
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPT
standard template library(STL) in C++
PDF
Python programming : List and tuples
PPT
Unit 3 graph chapter6
PDF
Tuples in Python
PPTX
Sequential & binary, linear search
PPTX
PPTX
Unit 4 python -list methods
PPTX
Recursive Function
PDF
Time and Space Complexity
PPTX
Data types in python
PPT
Python List.ppt
PPTX
Python-List.pptx
Python strings
Lec 17 heap data structure
Data structure - Graph
Python programming : Strings
Queue data structure
Python Libraries and Modules
Data Structures - Lecture 8 [Sorting Algorithms]
standard template library(STL) in C++
Python programming : List and tuples
Unit 3 graph chapter6
Tuples in Python
Sequential & binary, linear search
Unit 4 python -list methods
Recursive Function
Time and Space Complexity
Data types in python
Python List.ppt
Python-List.pptx
Ad

Similar to Python list (20)

PDF
Python data handling notes
PDF
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
PPTX
Basic data structures in python
PPTX
Python _dataStructures_ List, Tuples, its functions
PPTX
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
PPTX
Chapter 3-Data structure in python programming.pptx
PDF
The Ring programming language version 1.10 book - Part 30 of 212
PDF
The Ring programming language version 1.9 book - Part 29 of 210
PDF
The Ring programming language version 1.8 book - Part 27 of 202
PPTX
Chapter 15 Lists
PPTX
UNIT-3 python and data structure alo.pptx
PDF
Python Data Types.pdf
PDF
Python Data Types (1).pdf
PDF
The Ring programming language version 1.5.3 book - Part 22 of 184
PPTX
List in Python
PPTX
Python for the data science most in cse.pptx
PPTX
11 Introduction to lists.pptx
PPTX
Data -structures for class 12 , easy ppt
DOCX
Python Materials- Lists, Dictionary, Tuple
Python data handling notes
Unit 1-Part-4-Lists, tuples and dictionaries.pdf
Basic data structures in python
Python _dataStructures_ List, Tuples, its functions
PYTHON-PROGRAMMING-UNIT-III.pptx kghbg kfhjf jruufg jtuuf
Chapter 3-Data structure in python programming.pptx
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.9 book - Part 29 of 210
The Ring programming language version 1.8 book - Part 27 of 202
Chapter 15 Lists
UNIT-3 python and data structure alo.pptx
Python Data Types.pdf
Python Data Types (1).pdf
The Ring programming language version 1.5.3 book - Part 22 of 184
List in Python
Python for the data science most in cse.pptx
11 Introduction to lists.pptx
Data -structures for class 12 , easy ppt
Python Materials- Lists, Dictionary, Tuple
Ad

More from Prof. Dr. K. Adisesha (20)

PDF
MACHINE LEARNING Notes by Dr. K. Adisesha
PDF
Probabilistic and Stochastic Models Unit-3-Adi.pdf
PDF
Genetic Algorithm in Machine Learning PPT by-Adi
PDF
Unsupervised Machine Learning PPT Adi.pdf
PDF
Supervised Machine Learning PPT by K. Adisesha
PDF
Introduction to Machine Learning PPT by K. Adisesha
PPSX
Design and Analysis of Algorithms ppt by K. Adi
PPSX
Data Structure using C by Dr. K Adisesha .ppsx
PDF
Operating System-4 "File Management" by Adi.pdf
PDF
Operating System-3 "Memory Management" by Adi.pdf
PDF
Operating System Concepts Part-1 by_Adi.pdf
PDF
Operating System-2_Process Managementby_Adi.pdf
PDF
Software Engineering notes by K. Adisesha.pdf
PDF
Software Engineering-Unit 1 by Adisesha.pdf
PDF
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
PDF
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
PDF
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
PDF
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
PDF
Computer Networks Notes by -Dr. K. Adisesha
PDF
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
MACHINE LEARNING Notes by Dr. K. Adisesha
Probabilistic and Stochastic Models Unit-3-Adi.pdf
Genetic Algorithm in Machine Learning PPT by-Adi
Unsupervised Machine Learning PPT Adi.pdf
Supervised Machine Learning PPT by K. Adisesha
Introduction to Machine Learning PPT by K. Adisesha
Design and Analysis of Algorithms ppt by K. Adi
Data Structure using C by Dr. K Adisesha .ppsx
Operating System-4 "File Management" by Adi.pdf
Operating System-3 "Memory Management" by Adi.pdf
Operating System Concepts Part-1 by_Adi.pdf
Operating System-2_Process Managementby_Adi.pdf
Software Engineering notes by K. Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Computer Networks Notes by -Dr. K. Adisesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Lesson notes of climatology university.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
01-Introduction-to-Information-Management.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Anesthesia in Laparoscopic Surgery in India
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial disease of the cardiovascular and lymphatic systems
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Paper A Mock Exam 9_ Attempt review.pdf.
Lesson notes of climatology university.
Module 4: Burden of Disease Tutorial Slides S2 2025
History, Philosophy and sociology of education (1).pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Classroom Observation Tools for Teachers

Python list

  • 1. List & Tuple Data in Python Prof. K. Adisesha| 1 Python Lists Python Collections (Arrays) 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. When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security. List: A list is a collection, which is ordered and changeable. In Python, lists are written with square brackets. Example Create a List: StuList = ["Prajwal", "Sunny", "Rekha"] print(StuList) Output: ['Prajwal', 'Sunny', 'Rekha'] Access Items: You access the list items by referring to the index number: Example Print the second item of the list: StuList = ["Prajwal", "Sunny", "Rekha"] print(StuList[1]) Output: Sunny Negative Indexing: Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc. Example Print the last item of the list: StuList = ["Prajwal", "Sunny", "Rekha"] print(StuList[-1]) Output: Rekha Range of Indexes: You can specify a range of indexes by specifying start and end position of the range.  When specifying a range, the return value will be a new list with the specified items. Example Return the third, fourth, and fifth item: StuList = ["Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu"] print(StuList[2:5])
  • 2. List & Tuple Data in Python Prof. K. Adisesha| 2 Output: ["Rekha", "Sam", "Adi"] Note: The search will start at index 2 (included) and end at index 5 (not included). Range of Negative Indexes: Specify negative indexes if you want to start the search from the end of the list: Example This example returns the items from index -4 (included) to index -1 (excluded) StuList = ["Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu"] print(StuList[-4:-1]) Output: ["Sam", "Adi", "Ram"] Change Item Value: To change the value of a specific item, refer to the index number: Example Change the second item: StuList = ["Prajwal", "Sunny", "Rekha"] StuList[1] = "Adi" print(StuList) Output: ["Prajwal", "Adi", "Rekha"] Loop through a List: You can loop through the list items by using a for loop: Example Print all items in the list, one by one: StuList = ["Prajwal", "Sunny", "Rekha"] for x in StuList: print(x) Output: Prajwal Sunny Rekha Check if Item Exists: To determine if a specified item is present in a list use the in keyword: Example Check if “Sunny” is present in the list: StuList = ["Prajwal", "Sunny", "Rekha"] if "Prajwal" in StuList: print("Yes, 'Sunny' is in the Student list") Output: Yes, 'Sunny' is in the Student list List Length len() method: To determine how many items a list has use the: Example Print the number of items in the list: StuList = ["Prajwal", "Sunny", "Rekha"] print(len(StuList)) Output: 3
  • 3. List & Tuple Data in Python Prof. K. Adisesha| 3 Add Items: To add an item to the end of the list, use the append), insert(), method:  Using the append() method to append an item: Example StuList = ["Prajwal", "Sunny", "Rekha"] StuList.append("Sam") print(StuList) Output: ['Prajwal', 'Sunny', 'Rekha', 'Sam']  To add an item at the specified index, use the insert() method: Example Insert an item as the second position: StuList = ["Prajwal", "Sunny", "Rekha"] StuList.insert(1, "Sam") print(StuList) Output: ['Prajwal', 'Sam', 'Sunny', 'Rekha'] Remove Item: There are several methods to remove items from a list using: remove(), pop(), del, clear()  The remove() method removes the specified item: Example StuList = ["Prajwal", "Sunny", "Rekha"] StuList.remove("Sunny") print(StuList) Output: ['Prajwal', 'Rekha']  The pop() method removes the specified index, (or the last item if index is not specified): Example StuList = ["Prajwal", "Sunny", "Rekha"] StuList.pop() print(StuList) Output: ['Prajwal', 'Sunny']  The del keyword removes the specified index: Example StuList = ["Prajwal", "Sunny", "Rekha"] del StuList[0] print(StuList) Output: ['Sunny', 'Rekha']  The del keyword can also delete the list completely: Example StuList = ["Prajwal", "Sunny", "Rekha"] del StuList
  • 4. List & Tuple Data in Python Prof. K. Adisesha| 4  The clear() method empties the list: Example StuList = ["Prajwal", "Sunny", "Rekha"] StuList.clear() print(StuList) Copy a List: You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2. There are ways to make a copy, one-way is to use the built-in copy(), list() method.  copy() method: Make a copy of a list: Example StuList = ["Prajwal", "Sunny", "Rekha"] mylist = StuList.copy() print(mylist) Output: ['Prajwal', 'Sunny', 'Rekha']  list() method: Make a copy of a list: Example StuList = ["Prajwal", "Sunny", "Rekha"] mylist = list(StuList) print(mylist) Output: ['Prajwal', 'Sunny', 'Rekha'] Join Two Lists: There are several ways to join, or concatenate, two or more lists in Python.  One of the easiest ways are by using the + operator. Example list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list3 = list1 + list2 print(list3) Output: ['a', 'b', 'c', 1, 2, 3] Another way to join two lists are by appending all the items from list2 into list1, one by one: Example Append list2 into list1: list1 = ["a", "b”, "c"] list2 = [1, 2, 3] for x in list2: list1.append(x) print(list1) Output: ['a', 'b', 'c', 1, 2, 3]  Use the extend() method, which purpose is to add elements from one list to another list: Example
  • 5. List & Tuple Data in Python Prof. K. Adisesha| 5 Use the extend() method to add list2 at the end of list1: list1 = ["a", "b”, "c"] list2 = [1, 2, 3] list1.extend(list2) print(list1) Output: ['a', 'b', 'c', 1, 2, 3] The list() Constructor: It is also possible to use the list() constructor to make a new list. Example Using the list() constructor to make a List: StuList = list(("Prajwal", "Sunny", "Rekha")) # note the double round-brackets print(StuList) Output: ['Prajwal', 'Sunny', 'Rekha'] List Methods Python has a set of built-in methods that you can use on lists. Method Description append() Adds an element at the end of the list clear() Removes all the elements from the list copy() Returns a copy of the list count() Returns the number of elements with the specified value extend() Add the elements of a list (or any iterable), to the end of the current list index() Returns the index of the first element with the specified value insert() Adds an element at the specified position pop() Removes the element at the specified position remove() Removes the item with the specified value reverse() Reverses the order of the list sort() Sorts the list
  • 6. List & Tuple Data in Python Prof. K. Adisesha| 6 Python Tuples A tuple is a collection, which is ordered and unchangeable. In Python, tuples are written with round brackets. Example Create a Tuple: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) print(Stu_tuple) Output: ('Prajwal', 'Sunny', 'Rekha') Access Tuple Items: Tuple items access by referring to the index number, inside square brackets: Example Print the second item in the tuple: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) print(Stu_tuple[1]) Output: Sunny Negative Indexing: Negative indexing means beginning from the end, -1 refers to the last item, and -2 refers to the second last item etc. Example Print the last item of the tuple: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) print(Stu_tuple[-1]) Output: Rekha Range of Indexes: You can specify a range of indexes by specifying start and end of the range. When specifying a range, the return value will be a new tuple with the specified items. Example Return the third, fourth, and fifth item: Stu_tuple = ("Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu") print(Stu_tuple[2:5]) Output: (“Rekha", "Sam", "Adi”) Note: The search will start at index 2 (included) and end at index 5 (not included). Range of Negative Indexes: Specify negative indexes if you want to start the search from the end of the tuple: Example This example returns the items from index -4 (included) to index -1 (excluded) Stu_tuple = ("Prajwal", "Sunny", "Rekha", "Sam", "Adi", "Ram", "Shilu") print(Stu_tuple[-4:-1]) Output: ("Sam", "Adi", "Ram")
  • 7. List & Tuple Data in Python Prof. K. Adisesha| 7 Change Tuple Values: Once a tuple is created, you cannot change its values. Tuples are unchangeable or immutable as it also is called. However, by converting the tuple into a list, change the list, and convert the list back into a tuple. Example Convert the tuple into a list to be able to change it: x = (“Prajwal”, “Sunny”, “Rekha”) y = list(x) y[1] = "Adi" x = tuple(y) print(x) Output: (“Prajwal”, “Adi”, “Rekha”) Loop through a Tuple: You can loop through the tuple items by using a for loop. Example Iterate through the items and print the values: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) for x in Stu_tuple: print(x,”t”) Output: Prajwal Sunny Rekha Check if Item Exists: To determine if a specified item is present in a tuple use the in keyword: Example Check if "Prajwal" is present in the tuple: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) if "Prajwal" in Stu_tuple: print("Yes, 'Prajwal' is in the Student tuple") Output: Yes, 'Prajwal' is in the Student tuple Tuple Length: To determine how many items a tuple has, use the len() method: Example Print the number of items in the tuple: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) print(len(Stu_tuple)) Output: 3 Adding Items: Once a tuple is created, you cannot add items to it. Tuples are unchangeable. Example You cannot add items to a tuple: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) Stu_tuple[3] = "Adi" # This will raise an error print(Stu_tuple) Output: TypeError: 'tuple' object does not support item assignment Create Tuple With One Item: To create a tuple with only one item, you have add a comma after the item, unless Python will not recognize the variable as a tuple.
  • 8. List & Tuple Data in Python Prof. K. Adisesha| 8 Example One item tuple, remember the comma: Stu_tuple = ("Prajwal",) print(type(Stu_tuple)) Stu_tuple = ("Prajwal") #NOT a tuple print(type(Stu_tuple)) Output: <class 'tuple'> <class 'str'> Remove Items: Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely: Example The del keyword can delete the tuple completely: Stu_tuple = (“Prajwal”, “Sunny”, “Rekha”) del Stu_tuple print(Stu_tuple) #this will raise an error because the tuple no longer exists Join Two Tuples: To join two or more tuples you can use the + operator: Example Join two tuples: tuple1 = ("a", "b" , "c") tuple2 = (1, 2, 3) tuple3 = tuple1 + tuple2 print(tuple3) Output: ('a', 'b', 'c', 1, 2, 3) The tuple() Constructor: It is also possible to use the tuple() constructor to make a tuple. Example Using the tuple() method to make a tuple: Stu_tuple = tuple((“Prajwal”, “Sunny”, “Rekha”)) # note the double round-brackets print(Stu_tuple) Output: ('Prajwal', 'Sunny', 'Rekha') Tuple Methods Python has two built-in methods that you can use on tuples. Method Description count() Returns the number of times a specified value occurs in a tuple index() Searches the tuple for a specified value and returns the position of where it was found