A List is a kind of Collection
• A collection allows us to put many values in a single “variable”.(Simply
a container that are used to store a list of values )
• They are mutable(You can change the elements of a list and no new
list will be created.
• Adv: We can carry all many values around in one convenient package.
(lists can contain values of mixed data types.)
friends = [ 'Joseph', 'Glenn', 'Sally' ]
carryon = [ 'socks', 'shirt',
What is not a “Collection”
• Most of our variables have one value in them - when we put
a new value in the variable - the old value is over written
>>> x = 2
>>> x = 4
>>> print(x)
4
We have already used lists!
for i in [5, 4, 3, 2,
1] :
print(i)
print ('done!‘)
5
4
3
2
1
done!
LISTS
CREATING LIST:
Use square brackets.
Ex: A = [2, 4, 6]
Alpha = [‘abc’, ‘def’]
num = [1.0, 2, 3.5, 4]
L = [ ] # empty list, equivalent to 0 or ‘’(empty string).
(or)
[L = list[ ] Will create an empty list and name that as L
🡪
Long lists:
num = [0, 1, 2, 3, 4 ,5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20]
Nested lists:
add = [‘Rohan’,21,[“DLF”,”Akshay Nagar”],DPS South]
Add is a nested list with 4 elements. add[2] element is a list. Length of add is 4
Lists are Mutable
• Strings are "immutable" -
we cannot change the
contents of a string - we
must make a new string
to make any change
• Lists are "mutable" - we
can change an element of
a list using the index
operator
>>> fruit = 'Banana’
>>> fruit[0] = 'b’
Traceback
TypeError: 'str' object does not
support item assignment
>>> x = fruit.lower()
>>> print(x)
banana
>>> lotto = [2, 14, 26, 41, 63]
>>> lotto[2] = 28
>>> print(lotto)
[2, 14, 28, 41, 63]
Accessing lists:
■ List elements are indexed (2-way indexing)
■ Function len (str) return the number of items in the list str.
■ str[i] 🡪returns the item at index i (indexing)
■ str[i : j] 🡪returns a new list, containing the objects between i and j. (slicing)
■ Membership operators in, not in and concatenation operators + and * work on lists.
■ str[0] = ?
■ While accessing list elements, if you pass negative index, Python adds the length of the list
to the negative index to get element’s forward index. If index given outside the legal
indices[0 to length-1], Python raises index error.
■ str[2] = ‘t’ #To change the list element in place. ‘T’ changed to ‘t’ (becos lists are mutable)
CREATING LISTS FROM EXISTING SEQUENCES:
■ Creating lists via keyboard input:
l = list(input("Enter:"))
print(l, len(l),type(l))
l = eval(input("Enter:"))
print(l, type(l))
l = eval(input(“Enter:"))
print(l, type(l))
Note:
with input ( ),the
datatype of all
characters entered will
be treated as strings
Enter:4645fgfgdfgd
['4', '6', '4', '5', 'f', 'g', 'f', 'g', 'd', 'f', 'g', 'd'] 12 <class
'list'>
Enter:45464564
45464564 <class 'int'>
Enter:”Jayanagar”
45456456 <class ‘str'>
Traversing lists:
li = ['a', 'b', 'c', 'd', 'e']
for i in range(len(li)):
print(li[i])
li = ['a', 'b', 'c', 'd', 'e']
for i in li:
print(i)
for loop makes it easy to traverse (accessing and processing each element) or loop over the items in a
list
li is a list, and i will take the value of each element in turn, starting from the first element.
Printing the elements of a list along with element’s
both indices (positive and negative)
Joining
lists:
The ‘+’ operator when used with lists requires that both the operands must
be of list types. All these will result in an error.
list + number
list + complex number
list + string
Replicating
lists:
You can only use an integer with ‘*’ operator when trying to replicate a list.
The ‘*’ operator requires a list and an integer.
Intro to Robots
Objects and Values:
• Remember:
• So memory looks like:
• Two places in memory called x and y, both pointing to a place with a 3 stored
in it.
x = 3
y = 3
print id(x), id(y)
135045528 135045528
3
x
y
Intro to Robots
Lists, Objects and Values
• Lists are different:
• So this time the memory state picture is:
a = [1, 2, 3]
b = [1, 2, 3]
print id(a), id(b)
135023431 135024732
[1, 2, 3]
[1, 2, 3]
a
b
Intro to Robots
Aliasing
• However, if we assign one variable to another:
• So this time the memory state picture is:
• More importantly, changing b also changes a
a = [1, 2, 3]
b = a
print id(a), id(b)
135023431 135023431
[1, 2, 3]
a
b
b[0] = 0
print a
[0, 2, 3]
Slicing
lists:
seq = t [ start : stop ] creates a list slice seq out of list t with elements
falling between indexes start and stop, not including stop
Slicing is used to
retrieve a subset
of values
Python raises an IndexError Exception , if the resulting index is outside the
list in Normal Indexing.
lst1 = [1,2,3,4,5]
print(lst1[5])
lst1 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
print(lst1[3:30])
print(lst1[-18:11])
Slices are treated as boundaries
instead.
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20]
print(lst[0:10:2])
print("n")
print(lst[2:10:3])
print("n")
print(lst[::3])
print("n")
print(lst[5::2])
print("n")
print(lst[:10:2])
Using Slices for List Modification:
lst1 = ['one', 'two', 'three']
lst1[0:2] = [1, 2]
print(lst1)
lst1[0:2] = ['a']
print(lst1)
Extract 2 list slices out of a given list of numbers[1 to 20].
Display and print the sum of elements of 1st
list slice which
contains every other element of the list between index 5 to 15.
Also display the average of elements in 2nd
list slice that contains
every fourth element of the list.
WORKING WITH
LISTS:
APPENDING ELEMENTS TO A LIST:
lst.append(item) 🡪 Used to add single item at the END to an existing
sequence. It does not return the new list. Just modifies the original. Takes
exactly only one argument.
lst1 = [10,12,14]
lst1.append(16)
print(lst1)
lst1 = [10,12,14]
lst2 = lst1.append(16)
print(lst1)
print(lst2)
lst1 = [10,12,14]
lst1.append(16,18)
print(lst1)
extend () method:
🡪 Takes a list as an argument and appends all of the elements
of that list to the list to which extend() method is applied.
🡪 Like append( ), extend( ) also does not return any value.
lst1 = [1, 2, 3, 4, 5]
lst2 = [6, 7, 8]
lst1.extend(lst2)
print(lst1)
lst1 = [1, 2, 3, 4, 5]
#lst2 = [6, 7, 8]
lst1.extend([6, 7, 8, 9, 10])
print(lst1)
lst1 = [1, 2, 3, 4, 5]
lst2 = [6, 7, 8]
lst3 = lst1.extend(lst1)
print(lst3)
print(lst1)
T1 = [1,2,3]
T2 = [7,8]
T1.append(10)
Print(T1)
T1.append(12,14)
…….
T1.append([12,14])
….
Print(len(T1))
…..
T2.extend(10)
…..
T2.extend([12,14])
Print(T2)
T3 = [10,20]
T2.extend(T3)
Print(T2)
Print(len(T2))
Diff between
append() and
extend()
append( ) extend( )
It can add a single
element of the end of
a list.
It can add argument –
list’s all elements to
the end of the list.
After append( ), the
length of the list will
increase by 1
After extend( ), the
length of the list will
increase by the length
of the increased list.
DELETING ELEMENTS FROM A LIST:
del lst[index] Used to remove the element at the
🡪 given index. Can remove single element as well
as a list slice.
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
del(lst[1])
print(lst)
print("n")
del(lst[9:15])
print(lst)
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
rem = lst.pop(1)
print(rem)
lst.pop(10)
print("n")
print(lst)
pop( ) method:
The pop( ) method can remove only an individual item (not list slices).
It also returns the removed item along with deleting it from the list.
remove( ) method:
lst.remove(value) 🡪 Used to remove the first occurrence of the given value
from the list.
🡪 Used when you know the value of the
element to be removed , but you don’t
know its index or position in the list.
🡪 Takes one argument and does not return
anything.
lst1 = ['a', 'b', 'c', 'd', 'e']
lst1.remove('a')
print(lst1)
lst1 = ['a', 'b', 'c', 'd', 'e’,'a', 'b', 'c’]
lst1.remove('a')
print(lst1)
lst1 = ['a', 'b', 'c', 'd', 'e']
lst1.remove('f')
print(lst1)
TO MAKE A COPY OF A
LIST:
lst1 = [1, 2, 3, 4, 5]
lst2 = lst1 #This will not make lst2, a duplicate copy of lst1 .This
just makes label lst2 to point to where
label lst1 is pointing to.
1 2 3 4 5
lst
1
lst
2
lst1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
lst2 = lst1
print(lst2)
lst1[6] = 17
print(lst1)
print(lst2)
lst1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
lst2 = list(lst1)
print(lst2)
lst1[6] = 17
print(lst1)
print(lst2)
lst1 = [1, 2, 3, 4, 5]
lst2 = list(lst1) #True independent copy of a list is made via
list()method.
Assigning a list to another identifier just creates an alias for the
same list.
insert () method:
🡪 Used to insert an element in between or any position of
your choice.
🡪 Takes 2 arguments and returns no value
🡪 lst1.insert(pos, item)
t1 = ['a', 'e', 'o']
t1.insert(2, 'i')
print(t1)
t1 = ['a', 'e', 'o']
t1.insert(len(t1), 'u')
print(t1)
t1 = ['a', 'e', 'o']
t1.insert(100, 'u')
print(t1)
t1 = ['a', 'e', 'o']
t1.insert(-9, 'u')
print(t1)
reverse () method:
🡪 Reverses the items of the list.
🡪 This is done in place; it does not create a list.
🡪 lst.reverse( ) – Takes no argument, returns no list,
reverses the list in place.
lst1 = ['a', 'b', 'c', 'd', 'e']
lst1.reverse()
print(lst1)
lst1 = ['a', 'b', 'c', 'd', 'e']
print(lst1[::-1])
sort () method:
🡪 Sorts the items of the list, by default in increasing order.
🡪 This is done in place; it does not create a list.
🡪 lst.sort( ) – Takes no argument, returns no list,
reverses the list in place.
🡪 It will not sort the values of a list containing complex
numbers.
lst1 = [23, 45, 1, 90, 56, 24, 23]
lst1.sort()
print(lst1)
lst1 = [23, 45, 1, 90, 56, 24, 65, 23]
lst1.sort(reverse=True)
print(lst1)
QUESTIONS:
Write the most appropriate list method to
perform the following tasks:
L = [1,2,3,4,5,6,7,8,9,10]
1)Delete a given element say, 10 from the list.
2)Delete 3rd
element from the list.
3)Add an element to the end of the list.
4)Add an element in the beginning of the list.
5)Add elements of a list [11,12] in the end of
the list.
QUESTIONS:
Start with the list L = [8, 9, 10]. Do the
following:
1) Set the second entry to 17.
2) Add 4, 5, 6 to the end of the list.
3) Remove the first entry from the list.
4) Sort the list.
5) Double the list.
6) Insert 25 at index 3.
QUESTIONS:
1)Ask the user to enter a list containing
numbers between 1 and 12. Then replace all
the entries in the list that are greater than 10
with 10.
2) Print the smallest and largest integer in a list
called “num” without using loop
languages = ["C", "C++", "Perl", "Python"]
for x in languages:
... print(x)
O/P:
C
C++
Perl
Python
fibonacci = [0,1,1,2,3,5,8,13,21]
for i in range(len(fibonacci)):
print(i,fibonacci[i])
print(“n”)
Remark: If you apply len() to a list or a tuple, you get the number
of elements of this sequence.
# Python prog to illustrate the largest, second largest, smallest and second
smallest in a list
def find_len(list1):
length = len(list1)
list1.sort()
print("Largest element is:", list1[length-1])
print("Smallest element is:", list1[0])
print("Second Largest element is:", list1[length-2])
print("Second Smallest element is:", list1[1])
# Driver Code
list1=[12, 45, 2, 41, 31, 10, 8, 6, 4]
Largest = find_len(list1)
Python program to merge 2 lists and sort
them
Python program to swap the
first value and last value of a
SOLVE:
Create the following lists using a ‘for’ loop:
1) A list containing integers 0 to 49
2) A list containing the squares of integers 1 to 50
3) A list that [‘a’, ‘bb’, ‘ccc’,’dddd’,…………]ends with 26
copies of the letter z.
Write the output:
lst = [1,2,3]
print(lst * 3)
print(lst)
lst *= 3
print(lst)
Write the output:
l = ["Akash",454,56]
print(l[0][2])
Write the output:
l = [1,2]
m = 2
print(l*m)
Write the output:
l = [1,2]
m = 2
print(l+m)
What is the difference between:
append vs insert
pop vs remove
append vs extend
Write the output:
l = ["these",["are", "a"], ["few", "words"],"that","we","will","use"]
Write the output:
a = [1,2,3]
print(a + a + a)
print(a * 3)
a[0:2] = [9]
print(a)
print(len(l))
print(l[3:4]+l[1:2])
print("few" in l[2:3])
print("few" in l[2:3][0])
print("few" in l[2])
print(l[2][1:])
print(l[1]+l[2])
print(l[1][0 : : 2])
print(l[2][1][2] in l[1][0][1])

python Lists introduction where the students learn about how to use lists

  • 1.
    A List isa kind of Collection • A collection allows us to put many values in a single “variable”.(Simply a container that are used to store a list of values ) • They are mutable(You can change the elements of a list and no new list will be created. • Adv: We can carry all many values around in one convenient package. (lists can contain values of mixed data types.) friends = [ 'Joseph', 'Glenn', 'Sally' ] carryon = [ 'socks', 'shirt',
  • 2.
    What is nota “Collection” • Most of our variables have one value in them - when we put a new value in the variable - the old value is over written >>> x = 2 >>> x = 4 >>> print(x) 4
  • 3.
    We have alreadyused lists! for i in [5, 4, 3, 2, 1] : print(i) print ('done!‘) 5 4 3 2 1 done!
  • 4.
    LISTS CREATING LIST: Use squarebrackets. Ex: A = [2, 4, 6] Alpha = [‘abc’, ‘def’] num = [1.0, 2, 3.5, 4] L = [ ] # empty list, equivalent to 0 or ‘’(empty string). (or) [L = list[ ] Will create an empty list and name that as L 🡪 Long lists: num = [0, 1, 2, 3, 4 ,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Nested lists: add = [‘Rohan’,21,[“DLF”,”Akshay Nagar”],DPS South] Add is a nested list with 4 elements. add[2] element is a list. Length of add is 4
  • 5.
    Lists are Mutable •Strings are "immutable" - we cannot change the contents of a string - we must make a new string to make any change • Lists are "mutable" - we can change an element of a list using the index operator >>> fruit = 'Banana’ >>> fruit[0] = 'b’ Traceback TypeError: 'str' object does not support item assignment >>> x = fruit.lower() >>> print(x) banana >>> lotto = [2, 14, 26, 41, 63] >>> lotto[2] = 28 >>> print(lotto) [2, 14, 28, 41, 63]
  • 7.
    Accessing lists: ■ Listelements are indexed (2-way indexing) ■ Function len (str) return the number of items in the list str. ■ str[i] 🡪returns the item at index i (indexing) ■ str[i : j] 🡪returns a new list, containing the objects between i and j. (slicing) ■ Membership operators in, not in and concatenation operators + and * work on lists. ■ str[0] = ? ■ While accessing list elements, if you pass negative index, Python adds the length of the list to the negative index to get element’s forward index. If index given outside the legal indices[0 to length-1], Python raises index error. ■ str[2] = ‘t’ #To change the list element in place. ‘T’ changed to ‘t’ (becos lists are mutable)
  • 8.
    CREATING LISTS FROMEXISTING SEQUENCES:
  • 9.
    ■ Creating listsvia keyboard input: l = list(input("Enter:")) print(l, len(l),type(l)) l = eval(input("Enter:")) print(l, type(l)) l = eval(input(“Enter:")) print(l, type(l)) Note: with input ( ),the datatype of all characters entered will be treated as strings Enter:4645fgfgdfgd ['4', '6', '4', '5', 'f', 'g', 'f', 'g', 'd', 'f', 'g', 'd'] 12 <class 'list'> Enter:45464564 45464564 <class 'int'> Enter:”Jayanagar” 45456456 <class ‘str'>
  • 10.
    Traversing lists: li =['a', 'b', 'c', 'd', 'e'] for i in range(len(li)): print(li[i]) li = ['a', 'b', 'c', 'd', 'e'] for i in li: print(i) for loop makes it easy to traverse (accessing and processing each element) or loop over the items in a list li is a list, and i will take the value of each element in turn, starting from the first element. Printing the elements of a list along with element’s both indices (positive and negative)
  • 12.
    Joining lists: The ‘+’ operatorwhen used with lists requires that both the operands must be of list types. All these will result in an error. list + number list + complex number list + string
  • 13.
    Replicating lists: You can onlyuse an integer with ‘*’ operator when trying to replicate a list. The ‘*’ operator requires a list and an integer.
  • 14.
    Intro to Robots Objectsand Values: • Remember: • So memory looks like: • Two places in memory called x and y, both pointing to a place with a 3 stored in it. x = 3 y = 3 print id(x), id(y) 135045528 135045528 3 x y
  • 15.
    Intro to Robots Lists,Objects and Values • Lists are different: • So this time the memory state picture is: a = [1, 2, 3] b = [1, 2, 3] print id(a), id(b) 135023431 135024732 [1, 2, 3] [1, 2, 3] a b
  • 16.
    Intro to Robots Aliasing •However, if we assign one variable to another: • So this time the memory state picture is: • More importantly, changing b also changes a a = [1, 2, 3] b = a print id(a), id(b) 135023431 135023431 [1, 2, 3] a b b[0] = 0 print a [0, 2, 3]
  • 17.
    Slicing lists: seq = t[ start : stop ] creates a list slice seq out of list t with elements falling between indexes start and stop, not including stop
  • 18.
    Slicing is usedto retrieve a subset of values Python raises an IndexError Exception , if the resulting index is outside the list in Normal Indexing. lst1 = [1,2,3,4,5] print(lst1[5]) lst1 = [1,2,3,4,5,6,7,8,9,10,11,12,13] print(lst1[3:30]) print(lst1[-18:11]) Slices are treated as boundaries instead.
  • 20.
    lst = [1,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] print(lst[0:10:2]) print("n") print(lst[2:10:3]) print("n") print(lst[::3]) print("n") print(lst[5::2]) print("n") print(lst[:10:2])
  • 21.
    Using Slices forList Modification: lst1 = ['one', 'two', 'three'] lst1[0:2] = [1, 2] print(lst1) lst1[0:2] = ['a'] print(lst1) Extract 2 list slices out of a given list of numbers[1 to 20]. Display and print the sum of elements of 1st list slice which contains every other element of the list between index 5 to 15. Also display the average of elements in 2nd list slice that contains every fourth element of the list.
  • 22.
    WORKING WITH LISTS: APPENDING ELEMENTSTO A LIST: lst.append(item) 🡪 Used to add single item at the END to an existing sequence. It does not return the new list. Just modifies the original. Takes exactly only one argument. lst1 = [10,12,14] lst1.append(16) print(lst1) lst1 = [10,12,14] lst2 = lst1.append(16) print(lst1) print(lst2) lst1 = [10,12,14] lst1.append(16,18) print(lst1)
  • 23.
    extend () method: 🡪Takes a list as an argument and appends all of the elements of that list to the list to which extend() method is applied. 🡪 Like append( ), extend( ) also does not return any value. lst1 = [1, 2, 3, 4, 5] lst2 = [6, 7, 8] lst1.extend(lst2) print(lst1) lst1 = [1, 2, 3, 4, 5] #lst2 = [6, 7, 8] lst1.extend([6, 7, 8, 9, 10]) print(lst1) lst1 = [1, 2, 3, 4, 5] lst2 = [6, 7, 8] lst3 = lst1.extend(lst1) print(lst3) print(lst1)
  • 24.
    T1 = [1,2,3] T2= [7,8] T1.append(10) Print(T1) T1.append(12,14) ……. T1.append([12,14]) …. Print(len(T1)) ….. T2.extend(10) ….. T2.extend([12,14]) Print(T2) T3 = [10,20] T2.extend(T3) Print(T2) Print(len(T2)) Diff between append() and extend()
  • 25.
    append( ) extend() It can add a single element of the end of a list. It can add argument – list’s all elements to the end of the list. After append( ), the length of the list will increase by 1 After extend( ), the length of the list will increase by the length of the increased list.
  • 26.
    DELETING ELEMENTS FROMA LIST: del lst[index] Used to remove the element at the 🡪 given index. Can remove single element as well as a list slice. lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] del(lst[1]) print(lst) print("n") del(lst[9:15]) print(lst) lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] rem = lst.pop(1) print(rem) lst.pop(10) print("n") print(lst) pop( ) method: The pop( ) method can remove only an individual item (not list slices). It also returns the removed item along with deleting it from the list.
  • 27.
    remove( ) method: lst.remove(value)🡪 Used to remove the first occurrence of the given value from the list. 🡪 Used when you know the value of the element to be removed , but you don’t know its index or position in the list. 🡪 Takes one argument and does not return anything. lst1 = ['a', 'b', 'c', 'd', 'e'] lst1.remove('a') print(lst1) lst1 = ['a', 'b', 'c', 'd', 'e’,'a', 'b', 'c’] lst1.remove('a') print(lst1) lst1 = ['a', 'b', 'c', 'd', 'e'] lst1.remove('f') print(lst1)
  • 28.
    TO MAKE ACOPY OF A LIST: lst1 = [1, 2, 3, 4, 5] lst2 = lst1 #This will not make lst2, a duplicate copy of lst1 .This just makes label lst2 to point to where label lst1 is pointing to. 1 2 3 4 5 lst 1 lst 2 lst1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] lst2 = lst1 print(lst2) lst1[6] = 17 print(lst1) print(lst2)
  • 29.
    lst1 = [1,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] lst2 = list(lst1) print(lst2) lst1[6] = 17 print(lst1) print(lst2) lst1 = [1, 2, 3, 4, 5] lst2 = list(lst1) #True independent copy of a list is made via list()method. Assigning a list to another identifier just creates an alias for the same list.
  • 30.
    insert () method: 🡪Used to insert an element in between or any position of your choice. 🡪 Takes 2 arguments and returns no value 🡪 lst1.insert(pos, item) t1 = ['a', 'e', 'o'] t1.insert(2, 'i') print(t1) t1 = ['a', 'e', 'o'] t1.insert(len(t1), 'u') print(t1) t1 = ['a', 'e', 'o'] t1.insert(100, 'u') print(t1) t1 = ['a', 'e', 'o'] t1.insert(-9, 'u') print(t1)
  • 31.
    reverse () method: 🡪Reverses the items of the list. 🡪 This is done in place; it does not create a list. 🡪 lst.reverse( ) – Takes no argument, returns no list, reverses the list in place. lst1 = ['a', 'b', 'c', 'd', 'e'] lst1.reverse() print(lst1) lst1 = ['a', 'b', 'c', 'd', 'e'] print(lst1[::-1])
  • 32.
    sort () method: 🡪Sorts the items of the list, by default in increasing order. 🡪 This is done in place; it does not create a list. 🡪 lst.sort( ) – Takes no argument, returns no list, reverses the list in place. 🡪 It will not sort the values of a list containing complex numbers. lst1 = [23, 45, 1, 90, 56, 24, 23] lst1.sort() print(lst1) lst1 = [23, 45, 1, 90, 56, 24, 65, 23] lst1.sort(reverse=True) print(lst1)
  • 36.
    QUESTIONS: Write the mostappropriate list method to perform the following tasks: L = [1,2,3,4,5,6,7,8,9,10] 1)Delete a given element say, 10 from the list. 2)Delete 3rd element from the list. 3)Add an element to the end of the list. 4)Add an element in the beginning of the list. 5)Add elements of a list [11,12] in the end of the list.
  • 37.
    QUESTIONS: Start with thelist L = [8, 9, 10]. Do the following: 1) Set the second entry to 17. 2) Add 4, 5, 6 to the end of the list. 3) Remove the first entry from the list. 4) Sort the list. 5) Double the list. 6) Insert 25 at index 3.
  • 38.
    QUESTIONS: 1)Ask the userto enter a list containing numbers between 1 and 12. Then replace all the entries in the list that are greater than 10 with 10. 2) Print the smallest and largest integer in a list called “num” without using loop
  • 39.
    languages = ["C","C++", "Perl", "Python"] for x in languages: ... print(x) O/P: C C++ Perl Python fibonacci = [0,1,1,2,3,5,8,13,21] for i in range(len(fibonacci)): print(i,fibonacci[i]) print(“n”) Remark: If you apply len() to a list or a tuple, you get the number of elements of this sequence.
  • 40.
    # Python progto illustrate the largest, second largest, smallest and second smallest in a list def find_len(list1): length = len(list1) list1.sort() print("Largest element is:", list1[length-1]) print("Smallest element is:", list1[0]) print("Second Largest element is:", list1[length-2]) print("Second Smallest element is:", list1[1]) # Driver Code list1=[12, 45, 2, 41, 31, 10, 8, 6, 4] Largest = find_len(list1)
  • 42.
    Python program tomerge 2 lists and sort them
  • 43.
    Python program toswap the first value and last value of a
  • 45.
    SOLVE: Create the followinglists using a ‘for’ loop: 1) A list containing integers 0 to 49 2) A list containing the squares of integers 1 to 50 3) A list that [‘a’, ‘bb’, ‘ccc’,’dddd’,…………]ends with 26 copies of the letter z. Write the output: lst = [1,2,3] print(lst * 3) print(lst) lst *= 3 print(lst)
  • 47.
    Write the output: l= ["Akash",454,56] print(l[0][2]) Write the output: l = [1,2] m = 2 print(l*m) Write the output: l = [1,2] m = 2 print(l+m) What is the difference between: append vs insert pop vs remove append vs extend Write the output: l = ["these",["are", "a"], ["few", "words"],"that","we","will","use"] Write the output: a = [1,2,3] print(a + a + a) print(a * 3) a[0:2] = [9] print(a) print(len(l)) print(l[3:4]+l[1:2]) print("few" in l[2:3]) print("few" in l[2:3][0]) print("few" in l[2]) print(l[2][1:]) print(l[1]+l[2]) print(l[1][0 : : 2]) print(l[2][1][2] in l[1][0][1])