List Operations in Python

Good morning (or good
afternoon)
Cat powered up
https://forms.gle/zunuX4mP
N1h43yud8
Open session
with the class
Questions for discussion
What are the
die-die must
know things?
Abstraction
Not the art, but to reduce to minimum details
What is a list
A list may comprise of numbers, strings, etc
You may manually define a list using square brackets e.g. L =
[a,b,c,d]
Functions may be directly performed on a list using
L.function_name()
List operations
forward/reverse sorting, positional indexing, appending, deleting,
slicing, subsetting, etc
For accessing elements via positional indexing, we may do so via the
expression L[start:end]
Note: Leave <end> blank if we want it to go all the way to the end
of the list
Note: the final index is not returned
A list is
simply… an
ordered
arrangement
of elements
(items)
This Photo
 by Unknown Author is licensed under 
CC BY-SA
Initializing a
list in python
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"]
Updating
elements in
list
Delete List
Elements
Basic List
Operations
+ and * operators mean
concatenation and
repetition
Indexing and
Slicing
L = ['spam', 'Spam',
'SPAM!']
List functions
list1 = ['spam', 'Spam',
'SPAM!’]
list2 = [‘spam’, ‘spanner’,
‘spammer’]
list = [100,10, 1000,0]
List methods
list = ['spam', 'Spam',
'SPAM!’]
obj = ‘spam’
seq = [‘spammer’, ‘spanner’,
‘spin’]
List methods
 
Ready?
Now let’s start the
tutorial proper
1
.
 
D
e
f
i
n
e
 
t
h
e
 
l
i
s
t
L
 
=
 
[
3
4
,
 
2
,
 
4
5
,
 
1
0
,
-
2
,
 
6
]
.
 
T
h
e
n
,
 
d
o
t
h
e
 
f
o
l
l
o
w
i
n
g
a
c
t
i
o
n
s
:
1
.
 
D
e
f
i
n
e
 
t
h
e
 
l
i
s
t
 
L
 
=
 
[
3
4
,
 
2
,
 
4
5
,
 
1
0
,
 
-
2
,
 
6
]
.
 
T
h
e
n
,
 
d
o
 
t
h
e
 
f
o
l
l
o
w
i
n
g
 
a
c
t
i
o
n
s
:
D
a
t
a
 
a
b
s
t
r
a
c
t
i
o
n
 
 
c
o
m
p
o
s
i
t
e
 
t
y
p
e
s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
d
i
s
p
l
a
y
 
t
h
e
 
s
u
b
-
l
i
s
t
 
f
r
o
m
 
t
h
e
 
3
r
d
 
t
o
 
t
h
e
l
a
s
t
 
e
l
e
m
e
n
t
 
i
n
c
l
u
d
e
d
S
o
l
u
t
i
o
n
:
p
r
i
n
t
(
L
[
2
:
]
)
2. What is printed on the screen when you execute the following commands?
Data Abstraction - Strings in Python.pdf (slide 13)
Sa = 'abcdefghij'
Sb = Sa[:4]*2 + Sa[6:]*2 + Sa[5]
What do we know?
In Python, a string may also be treated as a collection of elements.
In this case, the string is a collection of characters
We may perform manipulation on a string via position indexing and
even “arithmetic operations”
Addition in string manipulation terms means to append (e.g. “abc” +
“efg” = “abcefg”)
Multiplication in string manipulation terms means to duplicate (e.g.
“abc” * 2 = “abcabc”)
2. What is printed on the screen
when you execute the following
commands?
Data Abstraction - Strings in
Python.pdf (slide 13)
Sa = 'abcdefghij'
Sb = Sa[:4]*2 + Sa[6:]*2 + Sa[5]
Sa[:4] is ‘
abcd
Sa[:4]*2 is ‘
abcdabcd
Sa[6:] is ‘
ghij
Sa[6:]*2 is ‘
ghijghij
Sa[5] is ‘
f
Solution:
Sa[:4]*2 + Sa[6:]*2 + Sa[5]
 is
abcdabcd
ghijghij
f’
2. What is printed on the screen
when you execute the following
commands?
Data Abstraction - Strings in
Python.pdf (slide 13)
Sa = 'abcdefghij'
Sb = Sa[:4]*2 + Sa[6:]*2 + Sa[5]
Sa[:4] is ‘
abcd
Sa[:4]*2 is ‘
abcdabcd
Sa[6:] is ‘
ghij
Sa[6:]*2 is ‘
ghijghij
Sa[5] is ‘
f
Solution:
Sa[:4]*2 + Sa[6:]*2 + Sa[5]
 is
abcdabcd
ghijghij
f’
Additional
3
.
 
U
s
i
n
g
 
t
h
e
r
a
n
g
e
f
u
n
c
t
i
o
n
(
s
)
,
c
r
e
a
t
e
 
t
h
e
f
o
l
l
o
w
i
n
g
 
l
i
s
t
s
:
What do we know?
The range function can be used to specify a
start, end, and interval in the form
range(start, end, interval)
It is important to note that the end is not
included in the return
The interval can be position or negative.
If positive, end > start, and it is an
incremental series
If negative, start > end, and it is a
decremental series
3
.
 
U
s
i
n
g
 
t
h
e
r
a
n
g
e
f
u
n
c
t
i
o
n
(
s
)
,
c
r
e
a
t
e
 
t
h
e
f
o
l
l
o
w
i
n
g
 
l
i
s
t
s
:
Solution
:
(a) [3, 4, 5, 6]
list(range(3,7))
3
.
 
U
s
i
n
g
 
t
h
e
r
a
n
g
e
f
u
n
c
t
i
o
n
(
s
)
,
c
r
e
a
t
e
 
t
h
e
f
o
l
l
o
w
i
n
g
 
l
i
s
t
s
:
Solution
:
(b) [18, 16, 14, 12]
Solution
:
list(range(18,10,-2))
(c) [50, 45, 40, 35, 30, 5, 10, 15, 20, 25, 30]
3
.
 
U
s
i
n
g
 
t
h
e
r
a
n
g
e
f
u
n
c
t
i
o
n
(
s
)
,
c
r
e
a
t
e
 
t
h
e
f
o
l
l
o
w
i
n
g
 
l
i
s
t
s
:
Solution
:
(c) [50, 45, 40, 35, 30, 5, 10, 15, 20, 25, 30]
Solution
:
list(range(50,25,-5)) + list(range(5,35,5))
4. Assume you are given two lists, myList1
and myList2, both of unknown sizes (for
testing purposes, you can create your own).
Do the following:
(a) Using only methods from lists, remove
the last element from myList1, remove the
first element from myList2, concatenate these
two new lists while adding a string element
'Hello' in between. (Composite types – slide
23)
4. Assume you are given two lists, myList1 and myList2, both of
unknown sizes (for testing purposes, you can create your own). 
Do the
following:
(a) Using only methods from lists, remove the last element from
myList1, remove the first element from myList2, concatenate these two
new lists while adding a string element 'Hello' in between. (Composite
types – slide 23)
Solution:
myList1.pop()
myList2.pop(0)
myList1.append('Hello')
myList1.extend(myList2)
(b) Same question, without using any list method. Indexing only!
4. Assume you are given two lists, myList1 and myList2,
both of unknown sizes (for testing purposes, you can
create your own). 
Do the following:
(a) Using only methods from lists, remove the last
element from myList1, remove the first element from
myList2, concatenate these two new lists while adding a
string element 'Hello' in between. (Composite types – slide
23)
(b) Same question, without using any list method.
Indexing only!
Solution:
myList1[:len(myList1)-1] + ['Hello'] + myList2[1:]
You may leave once you complete
the items. Or stick around and
chat/discuss
Emphasis: Must try to work with and
within your teams.
Please enjoy today’s practical
Slide Note
Embed
Share

A comprehensive guide to list operations in Python, covering topics such as defining lists, performing functions on lists, indexing, slicing, and more. Learn about the basics of lists, initializing lists, updating elements within a list, deleting list elements, and common list operations like concatenation and repetition.

  • Python
  • Lists
  • Operations
  • Indexing
  • Slicing

Uploaded on Sep 12, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Good morning (or good afternoon) Cat powered up https://forms.gle/zunuX4mP N1h43yud8

  2. Open session with the class Questions for discussion

  3. Abstraction Not the art, but to reduce to minimum details What is a list A list may comprise of numbers, strings, etc You may manually define a list using square brackets e.g. L = [a,b,c,d] Functions may be directly performed on a list using L.function_name() What are the die-die must know things? List operations forward/reverse sorting, positional indexing, appending, deleting, slicing, subsetting, etc For accessing elements via positional indexing, we may do so via the expression L[start:end] Note: Leave <end> blank if we want it to go all the way to the end of the list Note: the final index is not returned

  4. A list is simply an ordered arrangement of elements (items) This Photo by Unknown Author is licensed under CC BY-SA

  5. written as a list of comma- separated values (items) between square brackets. Initializing a list in python Items in a list need not be of the same type. list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]

  6. List list1 = ['physics', 'chemistry', 1997, 2000]; Print print "Value available at index 2 : " Print print list1[2] Updating elements in list List list1[2] = 2001; Print print "New value available at index 2 : " Print print list1[2]

  7. list1 = ['physics', 'chemistry', 1997, 2000]; print list1 Delete List Elements del list1[2]; print "After deleting value at index 2 : " print list1

  8. Python Expression Results Description + and * operators mean concatenation and repetition len([1, 2, 3]) 3 Length [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation Basic List Operations ['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition 3 in [1, 2, 3] True Membership for x in [1, 2, 3]: print x, 1 2 3 Iteration

  9. PYTHON EXPRESSION RESULTS DESCRIPTION L = ['spam', 'Spam', 'SPAM!'] L[2] SPAM! Offsets start at zero Indexing and Slicing L[-2] Spam Negative: count from the right L[1:] ['Spam', 'SPAM!'] Slicing fetches sections

  10. Sr.No. Function with Description 1 cmp(list1, list2) #Compares elements of both lists. Python 2 only List functions 2 len(list) #Gives the total length of the list. list1 = ['spam', 'Spam', 'SPAM! ] list2 = [ spam , spanner , spammer ] list = [100,10, 1000,0] 3 max(list) #Returns item from the list with max value. 4 min(list) #Returns item from the list with min value. 5 list(seq) #Converts a tuple into list.

  11. 1 list.append(obj) #Appends object obj to list 2 list.count(obj) #Returns count of how many times obj occurs in list List methods 3 list.extend(seq) #Appends the contents of seq to list list = ['spam', 'Spam', 'SPAM! ] obj = spam seq = [ spammer , spanner , spin ] 4 list.index(obj) #Returns the lowest index in list that obj appears 5 list.insert(index, obj) #Inserts object obj into list at offset index

  12. 6 list.pop(obj=list[-1])Removes returns last object or obj from list and List methods 7 list.remove(obj)Removes object obj from list 8 list.reverse()Reverses objects of list in place 9 list.sort([func])Sorts objects of list, use compare func if given

  13. Ready? Now let s start the tutorial proper

  14. What do we know? A list is a ordered collection of elements Because it is ordered, position matters 1. Define the list 1. Define the list L = [34, 2, 45, 10, L = [34, 2, 45, 10, - -2, 6]. Then, do 2, 6]. Then, do the following the following actions: actions: We may therefore access elements in a collection by its positional index Functions may be directly performed on a list using L.function_name() Where function_name includes sort, reverse, append For accessing elements via positional indexing, we may do so via the expression L[start:end] You may retrieve the position index of an element by exact search using the L.index(<element name>) function Like a loan shark, once I know your address (position index), I can do something to you.

  15. 1. Define the list L = [34, 2, 45, 10, -2, 6]. Then, do the following actions: Data abstraction composite types in Python (slide 23). (a) sort and print the list Solution: L.sort() print(L) (e) remove the element 10 and print the list Solution: del L[L.index(10)] print(L) (b) add element 13 at the end of the list and print the list Solution: L.append(13) print(L) (f) display the sub-list from the 2nd to the 3rd element included Solution: print(L[1:3]) (c) reverse the order of the element in the list and print the list Solution: L.reverse() print(L) (g) display the sub-list from the 3rd to the last element included Solution: print(L[2:]) (d) display the index of the element 34 Solution: my_index = L.index(34) print(my_index)

  16. 2. What is printed on the screen when you execute the following commands? Data Abstraction - Strings in Python.pdf (slide 13) Sa = 'abcdefghij' Sb = Sa[:4]*2 + Sa[6:]*2 + Sa[5] What do we know? In Python, a string may also be treated as a collection of elements. In this case, the string is a collection of characters We may perform manipulation on a string via position indexing and even arithmetic operations Addition in string manipulation terms means to append (e.g. abc + efg = abcefg ) Multiplication in string manipulation terms means to duplicate (e.g. abc * 2 = abcabc )

  17. 2. What is printed on the screen when you execute the following commands? Data Abstraction - Strings in Python.pdf (slide 13) Sa[:4] is abcd Sa[:4]*2 is abcdabcd Sa[6:] is ghij Sa[6:]*2 is ghijghij Sa[5] is f Sa = 'abcdefghij' Sb = Sa[:4]*2 + Sa[6:]*2 + Sa[5] Solution: Sa[:4]*2 + Sa[6:]*2 + Sa[5] is abcdabcdghijghijf

  18. 2. What is printed on the screen when you execute the following commands? Data Abstraction - Strings in Python.pdf (slide 13) Sa[:4] is abcd Sa[:4]*2 is abcdabcd Sa[6:] is ghij Sa[6:]*2 is ghijghij Sa[5] is f Sa = 'abcdefghij' Sb = Sa[:4]*2 + Sa[6:]*2 + Sa[5] Solution: Sa[:4]*2 + Sa[6:]*2 + Sa[5] is abcdabcdghijghijf

  19. As biologists, you will realize that string manipulation is very important since gene function is determined by the sequence of ATGCs. Additional Unfortunately, not all the functions that work on a list necessarily works on a string. For example, you cannot direct reverse a string using string.reverse(). Instead, we have to rely on creative use of indexing. The expression below takes the first to 3rd positions of Sa, and then reverses it using the call [::-1] Sa[:4][::-1]

  20. What do we know? The range function can be used to specify a start, end, and interval in the form range(start, end, interval) It is important to note that the end is not included in the return The interval can be position or negative. If positive, end > start, and it is an incremental series If negative, start > end, and it is a decremental series 3. Using the 3. Using the range range function(s), function(s), create the create the following lists: following lists:

  21. 3. Using the 3. Using the range range function(s), function(s), create the create the following lists: following lists: Solution: (a) [3, 4, 5, 6] list(range(3,7))

  22. Solution: 3. Using the 3. Using the range range function(s), function(s), create the create the following lists: following lists: (b) [18, 16, 14, 12] Solution: list(range(18,10,-2)) (c) [50, 45, 40, 35, 30, 5, 10, 15, 20, 25, 30]

  23. 3. Using the 3. Using the range range function(s), function(s), create the create the following lists: following lists: Solution: (c) [50, 45, 40, 35, 30, 5, 10, 15, 20, 25, 30] Solution: list(range(50,25,-5)) + list(range(5,35,5))

  24. 4. Assume you are given two lists, myList1 and myList2, both of unknown sizes (for testing purposes, you can create your own). Do the following: (a) Using only methods from lists, remove the last element from myList1, remove the first element from myList2, concatenate these two new lists while adding a string element 'Hello' in between. (Composite types slide 23)

  25. 4. Assume you are given two lists, myList1 and myList2, both of unknown sizes (for testing purposes, you can create your own). Do the following: (a) Using only methods from lists, remove the last element from myList1, remove the first element from myList2, concatenate these two new lists while adding a string element 'Hello' in between. (Composite types slide 23) Solution: myList1.pop() myList2.pop(0) myList1.append('Hello') myList1.extend(myList2) (b) Same question, without using any list method. Indexing only!

  26. 4. Assume you are given two lists, myList1 and myList2, both of unknown sizes (for testing purposes, you can create your own). Do the following: (a) Using only methods from lists, remove the last element from myList1, remove the first element from myList2, concatenate these two new lists while adding a string element 'Hello' in between. (Composite types slide 23) (b) Same question, without using any list method. Indexing only! Solution: myList1[:len(myList1)-1] + ['Hello'] + myList2[1:]

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#