L
ISTS
Access Lists Items
list1 = ["abc", 34, True, 40, "male"]
print(list1)
print("Yes, '34' is in the fruits list")
list1[1] = "def"
print("List :",list1)
print("List :",list1)
print(list1)
list1.append("orange")
print(list1)
print(list1)
thistuple = ("kiwi", "apple")
list1.extend(thislist)
print(list1)
print(list1)
list1.remove("apple")
print(list1)
print(list1)
print(list1)
print(list1)
for x in list1:
print(x)
print(list1[k])
while i < len(list1):
print(list1[i])
i = i + 1
newlist = []
newlist.extend(list1)
print(newlist)
for x in list1:
if "a" in x:
newlist.append(x)
print(newlist)
list1.sort()
print(list1)
print(list1)
print(list1)
return abs(n - 50)
thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)
print(thislist)
print(list1)
thislist = list1.copy()
print(thislist)
print(thislist)
list1 = ["a", "b" , "c"]
thislist = [1, 2, 3]
list1.extend(thislist)
print(list1)
print(list2)
print("List Cleared:",list1)
ISTS
Access Lists Items
list1 = ["abc", 34, True, 40, "male"]
print(list1)
['abc', 34, True, 40, 'male']
print(list1[-1],list1[0])
male abc
print(list1[-3:],list1[3:])
[True, 40, 'male'] [40, 'male']
print(list1[:-3],list1[:3])
['abc', 34] ['abc', 34, True]
print(list1[-4:-1],list1[1:4])
[34, True, 40] [34, True, 40]
if 34 in list1:print("Yes, '34' is in the fruits list")
Yes, '34' is in the fruits list
Change Lists Itemslist1[1] = "def"
print("List :",list1)
List : ['abc', 'def', True, 40, 'male']
list1[1:3] = ["blackcurrant", "watermelon"]print("List :",list1)
List : ['abc', 'blackcurrant', 'watermelon', 40, 'male']
list1[0:5] =["watermelon"]print(list1)
['watermelon']
Add Lists Itemslist1.append("orange")
print(list1)
['watermelon', 'orange']
list1.insert(-3,"bananna")print(list1)
['bananna', 'watermelon', 'orange']
thislist = ["apple", "banana", "cherry"]thistuple = ("kiwi", "apple")
list1.extend(thislist)
print(list1)
['bananna', 'watermelon', 'orange', 'apple', 'banana', 'cherry']
list1.extend(thistuple)print(list1)
['bananna', 'watermelon', 'orange', 'apple', 'banana', 'cherry', 'kiwi', 'apple']
Remove Lists Itemslist1.remove("apple")
print(list1)
['bananna', 'watermelon', 'orange', 'banana', 'cherry', 'kiwi', 'apple']
list1.pop(1)print(list1)
['bananna', 'orange', 'banana', 'cherry', 'kiwi', 'apple']
list1.pop()print(list1)
['bananna', 'orange', 'banana', 'cherry', 'kiwi']
del list1[0]print(list1)
['orange', 'banana', 'cherry', 'kiwi']
Loop Lists Itemsfor x in list1:
print(x)
orange
banana
cherry
kiwi
for k in range(len(list1)):banana
cherry
kiwi
print(list1[k])
orange
banana
cherry
kiwi
i = 0banana
cherry
kiwi
while i < len(list1):
print(list1[i])
i = i + 1
orange
banana
cherry
kiwi
[print(j) for j in list1]banana
cherry
kiwi
orange
banana
cherry
kiwi
Lists Comprehensionbanana
cherry
kiwi
newlist = []
newlist.extend(list1)
print(newlist)
['orange', 'banana', 'cherry', 'kiwi']
newlist = []for x in list1:
if "a" in x:
newlist.append(x)
print(newlist)
['orange', 'banana']
print([x for x in list1 if "e" in x])
['orange', 'cherry']
print([x for x in range(10) if x < 2])
[0, 1]
print([x for x in list1])
['orange', 'banana', 'cherry', 'kiwi']
print(['hello' for x in list1])
['hello', 'hello', 'hello', 'hello']
print([x if x != "banana" else "orange" for x in list1])
['orange', 'orange', 'cherry', 'kiwi']
Sort Listslist1.sort()
print(list1)
['banana', 'cherry', 'kiwi', 'orange']
list1.sort(reverse = True)print(list1)
['orange', 'kiwi', 'cherry', 'banana']
list1.reverse()print(list1)
['banana', 'cherry', 'kiwi', 'orange']
def myfunc(n):return abs(n - 50)
thislist = [100, 50, 65, 82, 23]
thislist.sort(key = myfunc)
print(thislist)
[50, 65, 23, 82, 100]
list1.sort(key = str.upper)print(list1)
['banana', 'cherry', 'kiwi', 'orange']
Copy Liststhislist = list1.copy()
print(thislist)
['banana', 'cherry', 'kiwi', 'orange']
thislist = list(list1)print(thislist)
['banana', 'cherry', 'kiwi', 'orange']
Join Listslist1 = ["a", "b" , "c"]
thislist = [1, 2, 3]
list1.extend(thislist)
print(list1)
['a', 'b', 'c', 1, 2, 3]
list2 = list1 + thislistprint(list2)
['a', 'b', 'c', 1, 2, 3, 1, 2, 3]
list1.clear()print("List Cleared:",list1)
List Cleared: []
del list1 It will be deleted.
Comments
Post a Comment