0% found this document useful (0 votes)
18 views

List in Python

notes pyhton list question

Uploaded by

sangeeta1411309
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

List in Python

notes pyhton list question

Uploaded by

sangeeta1411309
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Python Lists & Tuple

Page 1

List

List Items
List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has index [1] etc.

Allow Duplicates

List Length

List Items - Data Types


Python Lists & Tuple
Page 2

type()

The list() Constructor

Python - Access List Items


Access Items
List items are indexed and you can access them by referring to the index number:

Negative Indexing
Python Lists & Tuple
Page 3

Range of Indexes

Range of Negative Indexes

Check if Item Exists

Python - Change List Items


Change Item Value
To change the value of a specific item, refer to the index number:
Python Lists & Tuple
Page 4

Change a Range of Item Values

Python - Add List Items


Append Items
o add an item to the end of the list, use the append() method:

Insert Items
To insert a list item at a specified index, use the insert() method.

The insert() method inserts an item at the specified index:


Python Lists & Tuple
Page 5

Extend List
To append elements from another list to the current list, use the extend() method.

Add Any Iterable


The extend() method does not have to append lists, you can add any iterable object
(tuples, sets, dictionaries etc.).

Python - Remove List Items


Remove Specified Item
Python Lists & Tuple
Page 6

Remove Specified Index


The pop() method removes the specified index.

Clear the List


The clear() method empties the list.

The list still remains, but it has no content.

Python - Loop Lists


Python Lists & Tuple
Page 7

Loop Through a List


you can loop through the list items by using a for loop:

Loop Through the Index Numbers


ou can also loop through the list items by referring to their index number.

Use the range() and len() functions to create a suitable iterable.

Using a While Loop


you can loop through the list items by using a while loop.

Use the len() function to determine the length of the list, then start at 0 and loop your
way through the list items by referring to their indexes.

Remember to increase the index by 1 after each iteration.


Python Lists & Tuple
Page 8

Looping Using List Comprehension


List Comprehension offers the shortest syntax for looping through lists:

Python - List Comprehension


List Comprehension
Python Lists & Tuple
Page 9

Python - Sort Lists


List objects have a sort() method that will sort the list alphanumerically,
ascending, by default:

Sort Descending
To sort descending, use the keyword argument reverse = True:

Case Insensitive Sort


By default the sort() method is case sensitive, resulting in all capital letters being sorted
before lower case letters:

Reverse Order
What if you want to reverse the order of a list, regardless of the alphabet?
Python Lists & Tuple
Page 10

The reverse() method reverses the current sorting order of the elements.

Python - Copy Lists

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 List method copy().

Python - Join Lists


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.


Python Lists & Tuple
Page 11

Python - List Methods


Python Lists & Tuple
Page 12

Python Tuples
mytuple = ("apple", "banana", "cherry")

Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
Python Lists & Tuple
Page 13

Ordered
When we say that tuples are ordered, it means that the items have a defined order, and
that order will not change.

Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after the
tuple has been created.

Allow Duplicates
Since tuples are indexed, they can have items with the same value:

Tuple Length
To determine how many items a tuple has, use the len() function:

Create Tuple With One Item


To create a tuple with only one item, you have to add a comma after the item, otherwise
Python will not recognize it as a tuple.
Python Lists & Tuple
Page 14
Python Lists & Tuple
Page 15

Tuple Items - Data Types


Tuple items can be of any data type:

type()
From Python's perspective, tuples are defined as objects with the data type 'tuple':

<class 'tuple'>

The tuple() Constructor


It is also possible to use the tuple() constructor to make a tuple.

Python - Access Tuple Items


Python Lists & Tuple
Page 16

Access Tuple Items


You can access tuple items by referring to the index number, inside square brackets:

Negative Indexing
Negative indexing means start from the end.

-1 refers to the last item, -2 refers to the second last item etc.

Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the
range.

When specifying a range, the return value will be a new tuple with the specified items.

Python - Update Tuples


Change Tuple Values
Once a tuple is created, you cannot change its values. Tuples are unchangeable,
or immutable as it also is called.

But there is a workaround. You can convert the tuple into a list, change the list, and
convert the list back into a tuple.
Python Lists & Tuple
Page 17

Add Items
Since tuples are immutable, they do not have a built-in append() method, but there are
other ways to add items to a tuple.

1. Convert into a list: Just like the workaround for changing a tuple, you can convert it
into a list, add your item(s), and convert it back into a tuple.

2. Add tuple to a tuple. You are allowed to add tuples to tuples, so if you want to
add one item, (or many), create a new tuple with the item(s), and add it to the existing
tuple:
Python Lists & Tuple
Page 18

Remove Items

Python - Unpack Tuples


Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is called "packing" a tuple:

Packing a tuple

Unpacking a tuple

Python - Loop Tuples


Python Lists & Tuple
Page 19

Loop Through a Tuple


You can loop through the tuple items by using a for loop.

Loop Through the Index Numbers


You can also loop through the tuple items by referring to their index number.

Use the range() and len() functions to create a suitable iterable.

Using a While Loop


You can loop through the tuple items by using a while loop.

Use the len() function to determine the length of the tuple, then start at 0 and loop your
way through the tuple items by referring to their indexes.

Remember to increase the index by 1 after each iteration.

Python - Join Tuples


Python Lists & Tuple
Page 20

Join Two Tuples


To join two or more tuples you can use the + operator:

Multiply Tuples
If you want to multiply the content of a tuple a given number of times, you can use
the * operator:

Python - Tuple Methods


Tuple Methods
Python has two built-in methods that you can use on tuples

You might also like