0% found this document useful (0 votes)
5 views8 pages

Module 2 - Data Scalability and Analytics

The document provides an overview of collection data types in Python, including lists, tuples, sets, and dictionaries, highlighting their characteristics and usage. It explains how to manipulate lists and tuples, including accessing items, negative indexing, slicing, and methods for adding, removing, and copying elements. The document emphasizes the importance of choosing the appropriate collection type based on the specific needs of the data set.

Uploaded by

zuprocyhannie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views8 pages

Module 2 - Data Scalability and Analytics

The document provides an overview of collection data types in Python, including lists, tuples, sets, and dictionaries, highlighting their characteristics and usage. It explains how to manipulate lists and tuples, including accessing items, negative indexing, slicing, and methods for adding, removing, and copying elements. The document emphasizes the importance of choosing the appropriate collection type based on the specific needs of the data set.

Uploaded by

zuprocyhannie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Python

Introduction
#19

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.

You access the list items by referring to the index number:

thislist = ["apple", "banana", "cherry"]


print(thislist[1])

Output:

Negative Indexing

Negative indexing means beginning from the end, -1 refers to the last
item, -2 refers to the second last item etc.
Python
Introduction
#20

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 list with the
specified items.

Return the third, fourth, and fifth item:

thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

Output:

 Remember that the first item has index 0.

By leaving out the start value, the range will start at the first item:

thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[:4])

Output:
Python
Introduction
#21

Loop Through a List

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

Print all items in the list, one by one:

thislist = ["apple", "banana", "cherry"]


for x in thislist:
print(x)

Output:

Check if Item Exists

To determine if a specified item is present in a list use the in keyword:

Check if "apple" is present in the list:

thislist = ["apple", "banana", "cherry"]


if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")

Output:

List Length

To determine how many items a list has, use the len() function:
Python
Introduction
#22

Print the number of items in the list:

thislist = ["apple", "banana", "cherry"]


print(len(thislist))

Output:

Add Items

To add an item to the end of the list, use the append() method.

To add an item at the specified index, use the insert() method.

Remove Item

There are several methods to remove items from a list:

The remove() method removes the specified item.

The pop() method removes the specified index, (or the last item if index
is not specified).

The del keyword removes the specified index.

The clear() method empties the list.

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().

Make a copy of a list with the copy() method:

thislist = ["apple", "banana", "cherry"]


mylist = thislist.copy()
print(mylist)
Python
Introduction
#23

Output:

Another way to make a copy is to use the built-in method list().

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.

list1 = ["a", "b" , "c"]


list2 = [1, 2, 3]

list3 = list1 + list2


print(list3)

Output:

Another way to join two lists are by appending all the items from list2 into
list1, one by one.

Or you can use the extend() method, which purpose is to add elements
from one list to another list.

The list() Constructor

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

Using the list() constructor to make a List:


Python
Introduction
#24

thislist = list(("apple", "banana", "cherry")) # note the


double round-brackets
print(thislist)

Output:

Python has a set of built-in methods that you can use on lists.

Python Tuples

Tuple

A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.

Access Tuple Items

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

Print the second item in the tuple:

thistuple = ("apple", "banana", "cherry")


print(thistuple[1])

Output:

Negative Indexing

Negative indexing means beginning 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.

Range of Negative Indexes

Specify negative indexes if you want to start the search from the end of
the tuple:

his example returns the items from index -4 (included) to index -1


(excluded)

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")


print(thistuple[-4:-1])

Output:
Python
Introduction
#26

Loop Through a Tuple

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

Iterate through the items and print the values:

thistuple = ("apple", "banana", "cherry")


for x in thistuple:
print(x)

Check if Item Exists

To determine if a specified item is present in a tuple use the in keyword:

Check if "apple" is present in the tuple:

thistuple = ("apple", "banana", "cherry")


if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")

Tuple Length

To determine how many items a tuple has, use the len() method.

Add Items

 Once a tuple is created, you cannot add items to it. Tuples


are unchangeable.

You might also like