a = ["apple", "banana", "cherry"]
b = ["Ford", "BMW", "Volvo"]
b = ["Ford", "BMW", "Volvo"]
a.append(b)
print(a)
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo']]
c=[10,20,30]
a.append(c)
print(a)
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo'], [10, 20, 30]]
=====================================================================
a.extend([100,200,300])
print(a)
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo'], [10, 20, 30], 100, 200, 300]
=====================================================================
a.insert(2,50)
print(a)
['apple', 'banana', 50, 'cherry', ['Ford', 'BMW', 'Volvo'], [10, 20, 30], 100, 200, 300]
=====================================================================
a.remove(50)
print(a)
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo'], [10, 20, 30], 100, 200, 300]
a.remove([10,20,30])
print(a)
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo'], 100, 200, 300]
a.pop()
300
print(a)
['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo'], 100, 200]
a.pop(3)
['Ford', 'BMW', 'Volvo']
print(a)
['apple', 'banana', 'cherry', 100, 200]
del a[2]
print(a)
['apple', 'banana', 100, 200]
del a
print(a)
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
print(a)
NameError: name 'a' is not defined
=====================================================================
cars = ['Ford', 'BMW', 'Volvo']
print(cars)
['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
['BMW', 'Ford', 'Volvo']
cars.sort(reverse=True)
print(cars)
['Volvo', 'Ford', 'BMW']
----------------------------
#example
# A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(key=myFunc)
print(cars)
Output:
['VW', 'BMW', 'Ford', 'Mitsubishi']
----------------------------
#example
# A function that returns the based on year
def myFunc(e):
return e['year']
cars = [
{'car': 'Ford', 'year': 2005},
{'car': 'Mitsubishi', 'year': 2000},
{'car': 'BMW', 'year': 2019},
{'car': 'VW', 'year': 2011}
]
cars.sort(key=myFunc)
print(cars)
Output:
[{'car': 'Mitsubishi', 'year': 2000}, {'car': 'Ford', 'year': 2005}, {'car': 'VW', 'year': 2011}, {'car': 'BMW', 'year': 2019}]
-----------------------
#example
# A function that returns the length of the value:
def myFunc(e):
return len(e)
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
cars.sort(reverse=True, key=myFunc)
print(cars)
Output:
['Mitsubishi', 'Ford', 'BMW', 'VW']
=====================================================================
a=[10,20,30]
print(a)
[10, 20, 30]
a=a*4
print(a)
[10, 20, 30, 10, 20, 30, 10, 20, 30, 10, 20, 30]
a.count(20)
4
a.pop()
30
a.count(30)
3
=====================================================================
fruits = ["apple", "banana", "cherry"]
x=fruits.copy()
print(x)
['apple', 'banana', 'cherry']
=====================================================================
fruits = ["apple", "banana", "cherry"]
x=fruits.index("cherry")
print(x)
2
a=[10,89,45,67,89]
b=a.index(89)
print(b)
1
=====================================================================
print(fruits)
['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)
['cherry', 'banana', 'apple']