List Flashcards
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
t = [“a”,”b”,”c”,”d”,”e”,”f”]
del t[1:5]
t
[‘a’, ‘f’]
s = “spam”
t = list(s)
t
[’s’, ‘p’, ‘a’, ‘m’]
s =”spam dog “
t = s.split()
t
What if we put s in split like s.split(s)
[‘spam’, ‘dog’]
[’’, ‘’]
t = [“pining”, “for”,”the”,”fjords”]
delimeter = “ “
s = delimeter.join(t)
s
‘pining for the fjords’
s = “spam-spam-spam”
delimiter = “-“
t = s.split(delimiter)
t
[‘spam’, ‘spam’, ‘spam’]
t= [“1”,”2”,”3”,”5”,”6”,”7”]
delimeter = “ “
s = delimeter.join(t)
s
‘1 2 3 5 6 7’
a = [1,2,3]
b = [1,2,3]
a is b
False
a = “banana”
b = “banana”
a is b
True
a = [1,2,3]
b = a
b is a
True
`a = [1,2,3]
b[0] = 42
a
[42, 2, 3]
def delete_head(t):
del t[0]
letter = [“a”,”b”,”c”]
delete_head(letter)
letter
[‘b’, ‘c’]
t1 = [1,2]
t2 = t1.append(3)
t1
What will t2 do?
[1, 2, 3]
None
t1 = [1,2]
t3 = t1 +[4]
t1 =
t3 =
t1 = [1,2,3]
t2 = [1,2,3,4]