python list functions and methods code example

Example 1: list methods python

list.append(x) # append x to end of list
list.extend(iterable) # append all elements of iterable to list
list.insert(i, x) # insert x at index i
list.remove(x) # remove first occurance of x from list
list.pop([i]) # pop element at index i (defaults to end of list)
list.clear() # delete all elements from the list
list.index(x[, start[, end]]) # return index of element x
list.count(x) # return number of occurances of x in list
list.reverse() # reverse elements of list in-place (no return)
list.sort(key=None, reverse=False) # sort list in-place
list.copy() # return a shallow copy of the list

Example 2: python list function

# empty list
print(list())

# vowel string
vowel_string = 'aeiou'
print(list(vowel_string))

# vowel tuple
vowel_tuple = ('a', 'e', 'i', 'o', 'u')
print(list(vowel_tuple))

# vowel list
vowel_list = ['a', 'e', 'i', 'o', 'u']
print(list(vowel_list))

Example 3: python list functions

# Method	Description
# append()	Adds an element at the end of the list
# clear()	Removes all the elements from the list
# copy()	Returns a copy of the list
# count()	Returns the number of elements with the specified value
# extend()	Add the elements of a list (or any iterable), to the end of the current list
# index()	Returns the index of the first element with the specified value
# insert()	Adds an element at the specified position
# pop()	Removes the element at the specified position
# remove()	Removes the first item with the specified value
# reverse()	Reverses the order of the list
# sort()	Sorts the list
# Note: Python does not have built-in support for Arrays, but Py# Method	Description
# append()	Adds an element at the end of the list
# clear()	Removes all the elements from the list
# copy()	Returns a copy of the list
# count()	Returns the number of elements with the specified value
# extend()	Add the elements of a list (or any iterable), to the end of the current list
# index()	Returns the index of the first element with the specified value
# insert()	Adds an element at the specified position
# pop()	Removes the element at the specified position
# remove()	Removes the first item with the specified value
# reverse()	Reverses the order of the list
# sort()	Sorts the list

Example 4: list methods python

list.append('Value') #Append to end

Example 5: python functions list

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True