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

03.1 Lists Dictionaries and Tuples (Part1)

This document provides an overview of lists in Python. It defines what a list is, how to create and access elements in a list, and how to modify lists by changing, adding, and removing elements. Specifically, it discusses: - Lists are ordered collections that can contain any type of object and are mutable. - Lists are created using square brackets and elements are separated by commas. Individual elements can be accessed by their index. - Elements can be modified by assigning a new value to an index, added using append() or insert(), and removed using del, pop(), or remove(). - Various list methods like append(), insert(), pop(), and remove() allow dynamically building, modifying, and manipulating

Uploaded by

Mohammad Asad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

03.1 Lists Dictionaries and Tuples (Part1)

This document provides an overview of lists in Python. It defines what a list is, how to create and access elements in a list, and how to modify lists by changing, adding, and removing elements. Specifically, it discusses: - Lists are ordered collections that can contain any type of object and are mutable. - Lists are created using square brackets and elements are separated by commas. Individual elements can be accessed by their index. - Elements can be modified by assigning a new value to an index, added using append() or insert(), and removed using del, pop(), or remove(). - Various list methods like append(), insert(), pop(), and remove() allow dynamically building, modifying, and manipulating

Uploaded by

Mohammad Asad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

LISTS, DICTIONARIES, AND TUPLES

13777 SPECIAL TOPICS IN HEALTH TECHNOLOGY


DR. AHMAD ALTAMIMI
OUTLINES

Lists Dictionaries Tuples


LISTS
WHAT IS A LIST?
 A list is a collection of items in a particular order.
 Lists can contain any sort of objects:
 numbers
 strings
 and even other lists.

 Because a list usually contains more than one element, it’s a good idea to make the
name of your list plural, such as letters, digits, or names.
 Unlike String, Lists objects are mutable. A mutable object is an object whose state can
be modified after it is created.
CREATING LIST
 In Python, square brackets ([]) indicate a list, and individual elements in the list are
separated by commas.
 Here’s a simple example of a list that contains four student names:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


print(names)

 If you ask Python to print a list, Python returns its representation of the list, including
the square brackets:

['mahmood', 'ahmad', 'abdullah', 'rania']


ACCESSING ELEMENTS IN A LIST
 Lists are ordered collections, so you can access any element in a list by telling Python
the position, or index, of the item desired.
 To access an element in a list, write the name of the list followed by the index of the
item enclosed in square brackets.
 For example, let’s pull out the first name in the list names:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


print(names[0])
 Python returns just that element without square brackets:

mahmood
USING STRING METHODS ON A STING ELEMENT IN A LIST
 You can also use the string methods on any sting element in a list.
 For example, you can format an element more neatly by using the title() method:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


print(names[0].title())

 This example produces the same output as the preceding example except 'Mahmood'
is capitalized.
INDEX POSITIONS START AT 0, NOT 1
 Python considers the first item in a list to be at position 0, not position 1.
 For example, to access the third item in a list, you request the item at index 2.

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


print(names[2].title())

 This code returns the value 'Abdullah'


ACCESSING THE LAST ELEMENT IN A LIST
 Python has a special syntax for accessing the last element in a list. By asking for the
item at index -1, Python always returns the last item in the list:
names = ['mahmood', 'ahmad', 'abdullah', 'rania']
print(names[-1].title())

 This code returns the value 'Rania'


 This syntax is quite useful, because you’ll often want to access the last items in a list
without knowing exactly how long the list is.
 This convention extends to other negative index values as well. The index -2 returns the second item
from the end of the list, the index -3 returns the third item from the end, and so forth.
USING INDIVIDUAL VALUES FROM A LIST
 You can use individual values from a list just as you would any other variable.
 For example, you can use f-strings to create a message based on a value from a list.

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


message = f"The first name is {names[0].title()}."
print(message)

 The output is a simple sentence about the first name in the list:

The first name is Mahmood.


PYTHON’S LISTS
EXERCISE 1

 Write a separate program to accomplish this exercise.


Save the program with a filename guest_list.py.
 Make a list that includes at least three friends you’d
like to invite to dinner.
 Then use your list to print a message to each person,
inviting them to dinner.
CHANGING, ADDING, AND REMOVING ELEMENTS
MODIFYING ELEMENTS IN A LIST
 Lists objects are mutable, which means you can change list elements after it is created.
 The syntax for modifying an element is similar to the syntax for accessing an element in
a list.
 To change an element, use the name of the list followed by the index of the element
you want to change, and then provide the new value you want that item to have.
 For example:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


print(names)
names[0] = 'bashayreh'
print(names)
ADDING ELEMENTS TO A LIST
 Python provides several ways to add new data to existing lists:
1. Appending Elements to the End of a List:
 When you append an item to a list by using the append() method, the new element is
added to the end of the list.
 For example, adds 'mohammad' to the end of the list without affecting any of the
other elements:
names = ['mahmood', 'ahmad', 'abdullah', 'rania'] The append()
names.append('mohammad') method can be
print(names) used to build Lists
 Output: dynamically!
['mahmood', 'ahmad', 'abdullah', 'rania', 'mohammad']
ADDING ELEMENTS TO A LIST
2. Inserting Elements into a List:
 You can add a new element at any position in your list by using the insert() method.
 You do this by specifying the index of the new element and the value of the new item.
 For example, adds 'mohammad' after the first element (1st position/Index) of the list:
names = ['mahmood', 'ahmad', 'abdullah', 'rania']
names.insert(1,'mohammad')
print(names)
 This operation shifts every other value in the list one position to the right:
 Output:
['mahmood', 'mohammad', 'ahmad', 'abdullah', 'rania']
REMOVING ELEMENTS FROM A LIST
 You can remove an item according to its position in the list or according to its value:
1. Removing an Item Using the del Statement:
 If you know the position of the item you want to remove from a list, you can use the
del statement.
 For Example, here’s how to remove the 2nd item (1st position/Index), 'ahmad', in the
list:
names = ['mahmood', 'ahmad', 'abdullah', 'rania']
del names[1]
print(names)
 Output:
['mahmood', 'abdullah', 'rania']
REMOVING ELEMENTS FROM A LIST
 In previous method, you can no longer access the value that was removed from the list
after the del statement is used.
2. Removing an Item Using the pop() Method:
 Sometimes you’ll want to use the value of an item after you remove it from a list.
 For example, you might want to remove a user from a list of active members and then
add that user to a list of inactive members.
 The pop() method removes the last item in a list, but it lets you work with that item after removing it.
 The term pop comes from thinking of a list as a stack of items and popping one item off the top of the
stack. In this analogy, the top of a stack corresponds to the end of a list.
REMOVING ELEMENTS FROM A LIST
 For example, let’s pop a name from the list of names:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


popped_name = names.pop()
print(popped_name)
print(names)

 Output:
rania
['mahmood', 'ahmad', 'abdullah']
REMOVING ELEMENTS FROM A LIST
3. Popping Items from any Position in a List:
 You can use pop() to remove an item from any position in a list by including the index
of the item you want to remove in parentheses.
 For example, you might want to remove the name from a list names:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


first_student_name = names.pop(0)
print(f"The first student name is {first_student_name.title()}.")
print(names)
 Output:
The first student name is Mahmood
['ahmad', 'abdullah', 'rania']
REMOVING ELEMENTS FROM A LIST
4. Removing an Item by Value:
 If you only know the value of the item you want to remove, you can use the remove()
method.
 For example, let’s remove the value 'ahmad' from the list of names:

names = ['mahmood', 'ahmad', 'abdullah', 'rania']


names.remove('ahmad')
print(names)

 Output:
['mahmood', 'abdullah', 'rania']
REMOVING ELEMENTS FROM A LIST
 If you’re unsure whether to use the del statement or the pop() method, here’s a simple
way to decide:
 when you want to delete an item from a list and not use that item in any way, use the del statement;
 if you want to use an item as you remove it, use the pop() method.

 The remove() method deletes only the first occurrence of the value you specify.
 If there’s a possibility the value appears more than once in the list, you’ll need to use a loop to make
sure all occurrences of the value are removed.
CHANGING, ADDING, AND REMOVING ELEMENTS
EXERCISE 2

 Write a separate program to accomplish this exercise.


Save the program with a filename
changing_guest_list.py
 You just heard that the 2nd person of your guests can’t
make dinner, so you need to update the list of invitees,
by inviting someone else.
 Start with your program from Exercise 1. Add a print() call at the
end of your program stating the name of the guest who can’t
make it.
 Modify your list, replacing the name of the guest who can’t make
it with the name of the new person you are inviting.
 Print a second set of invitation messages, one for each person
who is still on your list.
EXERCISE 3

 Write a separate program to accomplish this exercise.


Save the program with a filename more_guest.py
 You just found a bigger dinner table, so now more space is
available. Think of three more guests to invite to dinner.
 Start with your program from Exercise 2. Add a print() call to the
end of your program informing people that you found a bigger
dinner table.
 Use insert() to add one new guest to the beginning of your list.
 Use insert() to add one new guest to the middle of your list.
 Use append() to add one new guest to the end of your list.
 Print a new set of invitation messages, one for each person in your
list.
EXERCISE 4

 Write a separate program to accomplish this exercise. Save the


program with a filename shrinking_guest.py
 You just found out that your new dinner table won’t arrive in
time for the dinner, and you have space for only two guests.
 Start with your program from Exercise 3. Add a new line that prints a
message saying that you can invite only two people for dinner.
 Use pop() to remove guests from your list one at a time until only two
names remain in your list. Each time you pop a name from your list, print a
message to that person letting them know you’re sorry you can’t invite
them to dinner.
 Print a message to each of the two people still on your list, letting them
know they’re still invited.
 Use del to remove the last two names from your list, so you have an empty
list. Print your list to make sure you actually have an empty list at the end
of your program.

You might also like