Course Name: Programming for Big Data
Course Code: CSDS-4423
Credit Hours: 3
Instructor: Prof. Khalid Rasheed Ch.
Lecture #: 5
Today Agenda
List
Tuple
Set
Dictionary
•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, unchangeable*, and
unindexed. No duplicate members.
•Dictionary is a collection which is ordered** and changeable. No
duplicate members.
List
he list data type is a built-in data structure used to store collections of items. Lists are
ordered, mutable (modifiable), and can contain elements of different data types, including
integers, floats, strings, and even other lists. Lists are defined by enclosing the elements
within square brackets [ ] and separating them with commas.
Ordered: Lists maintain the order of elements as they are inserted. You can access elements
by their index, and they will be retrieved in the same order.
Mutable: Lists can be modified after they are created. You can change, add, or remove
elements from a list.
Heterogeneous: Lists can contain elements of different data types. For example, you can
have a list with integers, strings, and even nested lists.
Dynamic: Lists can grow or shrink in size dynamically as elements are added or removed.
Iterable: You can iterate over the elements of a list using loops or other iterable methods.
Declaring the List
thislist = ["apple", "banana", "cherry"]
print(thislist)
Access Items
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Negative Indexing
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range of Indexes
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
Change Item Value
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
Append Items
thislist = ["apple", "banana", "cherry"]
Remove Specified Item
thislist = ["apple", "banana", "cherry"]
[Link]("banana")
print(thislist)
Sort List Alphanumerically
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
[Link]()
print(thislist)
thislist = [100, 50, 65, 82, 23]
[Link]()
print(thislist)
Sort Descending
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
[Link](reverse = True)
print(thislist)
Join Two Lists
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
How to take values from user in list:
mylist=[]
n=int(input("enter a number"))
for i in range(n):
[Link](int(input("enter a number")))
for b in mylist:
print(b)
Tuple
tuple is another built-in data type used to store collections of items. Tuples are quite
similar to lists, but with one key difference: tuples are immutable, meaning once they are
created, their elements cannot be changed, added, or removed. Tuples are defined by
enclosing the elements within parentheses () and separating them with commas.
Immutable: Unlike lists, tuples cannot be modified after creation. Once a tuple is created,
you cannot change its elements.
Ordered: Like lists, tuples maintain the order of elements as they are inserted. You can
access elements by their index, and they will be retrieved in the same order.
Heterogeneous: Tuples can contain elements of different data types, just like lists.
Dynamic: Similar to lists, tuples can grow or shrink dynamically as elements are added or
removed.
Iterable: You can iterate over the elements of a tuple using loops or other iterable
methods.
Declaring the tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Tuple Length
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
Tuple Items - Data Types
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
Allow Duplicates
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
Access Tuple Items
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
Change Tuple Values
Tuples are unchangeable, meaning that you cannot change, add, or remove items once the
tuple is created. But
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Unpacking a Tuple
When we create a tuple, we normally assign values to it. This is called "packing" a
tuple:
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
Python - Loop Tuples
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
Python - Join Tuples
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
Set
set is a built-in data type that stores an unordered collection of unique elements. Sets
are mutable, meaning they can be changed after they are created, but they do not
allow duplicate values, which makes them especially useful for removing duplicates
from sequences and for mathematical operations like unions, intersections, and set
differences.
Unordered: The elements in a set do not have a defined order; you cannot rely on the
items being in any particular sequence.
Mutable: You can add or remove items from a set after it has been created.
No Duplicates: Any duplicate elements are automatically removed when added to a
set.
Declaring the set
thisset = {"apple", "banana", "cherry"} To Clear the Set
print(thisset) thisset = {"apple", "banana", "cherry"}
To get Length [Link]()
thisset = {"apple", "banana", "cherry"} print(thisset)
print(len(thisset)) To delete the set
Access items thisset = {"apple", "banana", "cherry"}
thisset = {"apple", "banana", "cherry"} del thisset
for x in thisset: print(thisset)
print(x) Union
_______________________ set1 = {"a", "b", "c"}
thisset = {"apple", "banana", "cherry"} set2 = {1, 2, 3}
print("banana" in thisset) set3 = [Link](set2)
_________________________ print(set3)
thisset = {"apple", "banana", "cherry"} Intersection
print("banana" not in thisset) set1 = {"apple", "banana", "cherry"}
Add item set2 = {"google", "microsoft", "apple"}
thisset = {"apple", "banana", "cherry"} set3 = [Link](set2)
[Link]("orange") print(set3)
print(thisset) Different
Remove Item set1 = {"apple", "banana", "cherry"}
thisset = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"}
[Link]("banana") set3 = [Link](set2)
print(thisset) print(set3)
Dictionary
Dictionary is a built-in data type that is mutable and used to store key-value pairs. It is
also known as an associative array or a hash table. Dictionaries are unordered
collections of items, meaning the items do not have a specific order, and they are
indexed by keys, not by their position. Here's a deeper look at Python dictionaries:
Key-Value pairs: Each item in a dictionary is a pair of a key and its corresponding
value.
Mutable: You can modify dictionaries by adding, removing, or updating key-value pairs
after they are created.
Unordered: The order of items in a dictionary is not guaranteed. Keys are used to
access values, not positions.
Keys are unique: Each key in a dictionary must be unique. However, the values in a
dictionary can be duplicate.
Access Item
thisdict = { Remove method
"brand": "Ford", thisdict = {
"model": "Mustang", "brand": "Ford",
"year": 1964 "model": "Mustang",
} "year": 1964
x = thisdict["model"] }
Change item [Link]("model")
thisdict = { print(thisdict)
"brand": "Ford", Loop
"model": "Mustang", for x in thisdict:
"year": 1964 print(x)
}
Copy Dictionary
thisdict["year"] = 2018
thisdict = {
Add Item "brand": "Ford",
thisdict = { "model": "Mustang",
"brand": "Ford", "year": 1964
"model": "Mustang", }
"year": 1964 mydict = [Link]()
} print(mydict)
thisdict["color"] = "red"
print(thisdict)