1list.append(x) # append x to end of list
2list.extend(iterable) # append all elements of iterable to list
3list.insert(i, x) # insert x at index i
4list.remove(x) # remove first occurance of x from list
5list.pop([i]) # pop element at index i (defaults to end of list)
6list.clear() # delete all elements from the list
7list.index(x[, start[, end]]) # return index of element x
8list.count(x) # return number of occurances of x in list
9list.reverse() # reverse elements of list in-place (no return)
10list.sort(key=None, reverse=False) # sort list in-place
11list.copy() # return a shallow copy of the list
1list.append()
2list.clear()
3list.copy()
4list.count()
5list.extend()
6list.index()
7list.insert()
8list.pop()
9list.remove()
10list.reverse()
11list.append()
12list.clear()
13list.copy()
14list.count()
15list.extend()
16list.index()
17list.insert()
18list.pop()
19list.remove()
20list.reverse()
21list.sort()
22sorted(list)
1my_list = ["banana", "cherry", "apple"]
2
3# len() : get the number of elements in a list
4print("Length:", len(my_list))
5
6# append() : adds an element to the end of the list
7my_list.append("orange")
8
9# insert() : adds an element at the specified position
10my_list.insert(1, "blueberry")
11print(my_list)
12
13# pop() : removes and returns the item at the given position, default is the last item
14item = my_list.pop()
15print("Popped item: ", item)
16
17# remove() : removes an item from the list
18my_list.remove("cherry") # Value error if not in the list
19print(my_list)
20
21# clear() : removes all items from the list
22my_list.clear()
23print(my_list)
24
25# reverse() : reverse the items
26my_list = ["banana", "cherry", "apple"]
27my_list.reverse()
28print('Reversed: ', my_list)
29
30# sort() : sort items in ascending order
31my_list.sort()
32print('Sorted: ', my_list)
33
34# use sorted() to get a new list, and leave the original unaffected.
35# sorted() works on any iterable type, not just lists
36my_list = ["banana", "cherry", "apple"]
37new_list = sorted(my_list)
38
39# create list with repeated elements
40list_with_zeros = [0] * 5
41print(list_with_zeros)
42
43# concatenation
44list_concat = list_with_zeros + my_list
45print(list_concat)
46
47# convert string to list
48string_to_list = list('Hello')
49print(string_to_list)
50
1# Method Description
2# append() Adds an element at the end of the list
3# clear() Removes all the elements from the list
4# copy() Returns a copy of the list
5# count() Returns the number of elements with the specified value
6# extend() Add the elements of a list (or any iterable), to the end of the current list
7# index() Returns the index of the first element with the specified value
8# insert() Adds an element at the specified position
9# pop() Removes the element at the specified position
10# remove() Removes the first item with the specified value
11# reverse() Reverses the order of the list
12# sort() Sorts the list
13# Note: Python does not have built-in support for Arrays, but Py# Method Description
14# append() Adds an element at the end of the list
15# clear() Removes all the elements from the list
16# copy() Returns a copy of the list
17# count() Returns the number of elements with the specified value
18# extend() Add the elements of a list (or any iterable), to the end of the current list
19# index() Returns the index of the first element with the specified value
20# insert() Adds an element at the specified position
21# pop() Removes the element at the specified position
22# remove() Removes the first item with the specified value
23# reverse() Reverses the order of the list
24# sort() Sorts the list
1#Objects can also contain methods. Methods in objects are functions that belong to the object.
2#Let us create a method in the Person class:
3
4class Person:
5
6 def __init__(self, name, age):
7 self.name = name
8 self.age = age
9
10 def myfunc(self): # This is a method
11 print("Hello my name is " + self.name)
12
13p1 = Person("John", 36)
14p1.myfunc()
1# empty list
2print(list())
3
4# vowel string
5vowel_string = 'aeiou'
6print(list(vowel_string))
7
8# vowel tuple
9vowel_tuple = ('a', 'e', 'i', 'o', 'u')
10print(list(vowel_tuple))
11
12# vowel list
13vowel_list = ['a', 'e', 'i', 'o', 'u']
14print(list(vowel_list))