List Flashcards
(37 cards)
t = [“d”,”c”,”e”,”b”,”a”]
t.sort()
t
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
t = [“a”,”b”,”c”]
t.append(“d”)
t
[‘a’, ‘b’, ‘c’, ‘d’]
t1 = [“a”,”b”,”c”]
t2= [“d”,”e”]
t1.extend(t2)
t1
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
t=[“a”,”b”,”c”,”d”,”e”,”f”]
t[1:3] = [“x”,”y”]
t
[‘a’, ‘x’, ‘y’, ‘d’, ‘e’, ‘f’]
t = [“a”,”b”,”c”,”d”,”e”,”f”]
t[:]
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
t = [“a”,”b”,”c”,”d”,”e”,”f”]
t[1:3]
[‘b’, ‘c’]
t = [“a”,”b”,”c”,”d”,”e”,”f”]
t[:4]
[‘a’, ‘b’, ‘c’, ‘d’]
t = [“a”,”b”,”c”,”d”,”e”,”f”]
t[3:]
[‘d’, ‘e’, ‘f’]
[1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[0] *4
[0, 0, 0, 0]
a = [1,2,3]
b = [4,5,6]
c = a+b
c
[1, 2, 3, 4, 5, 6]
number=[42,5]
for i in range(len(number)):
number[i] = number[i] * 2
print(number)
[84,10]
number=[42,5]
42 in number
123 in number
5 in number
TRUE
FALSE
TRUE
for cheese in cheeses:
print(cheese)
Cheddar
Edam
Gouda
Wil print without []
t = t.sort()
t
Return None and modify the List to empty list and if you try again you’ll get none type error because you are sorting empty list.
t = [1,2,3]
sum(t)
6
def capitalize_all(t):
res = []
for s in t:
res.append(s.capitalize())
return res
capitalize_all([“vishal”,”sehgal”])
Also,
What if we write last line without list like: capitalize_all(“vishal”,”sehgal”)
[‘Vishal’, ‘Sehgal’]
Error
.capitalize()
Capital 1st letter
.upper()
Capital Everything
.isupper()
Return True or False according to String.
This is not .upper which turn everything CAPITALISE
def only_upper(t):
res = []
for s in t:
if s.isupper():
res.append(s)
return res
only_upper([“vishal”,”sehgal”])
[]
Empty List
t = [“a”,”b”,”c”]
x = t.pop(1)
t
x
[‘a’, ‘c’]
‘b’
t= [“a”,”b”,”c”]
t.remove(“b”)
t
Will this work on index
[‘a’, ‘c’]
No, it only remove string
t = [“a”,”b”,”c”]
del t[1]
t
Will this work on String
[‘a’, ‘c’]
No, it only remove index