Flipkart Interview Question | What is the difference between extend and append function?

Question

.

in progress 0
TheDataMonk 55 years 6 Answers 1506 views Grand Master 0

Answers ( 6 )

  1. append: Appends object at end.

    x = [1, 2, 3]
    x.append([4, 5])
    print (x)
    gives you: [1, 2, 3, [4, 5]]

    extend: Extends list by appending elements from the iterable.

    x = [1, 2, 3]
    x.extend([4, 5])
    print (x)
    gives you: [1, 2, 3, 4, 5]

  2. append->append adds its argument as a single element to the end of a list. The length of the list itself will increase by one.
    Example-> my_list=[‘foo’, ‘bar’, ‘baz’]
    another_list = [1, 2, 3]
    my_list.append(another_list)
    print(my_list) => [‘foo’, ‘bar’, ‘baz’, [1, 2, 3]]

    extend-> extend iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.
    Example-> my_list=[‘foo’, ‘bar’]
    another_list = [1, 2, 3]
    my_list.extend(another_list)
    print(my_list)=> [‘foo’, ‘bar’, 1, 2, 3]
    my_list.extend(‘baz’)
    print(my_list)=> [‘foo’, ‘bar’, 1, 2, 3, ‘b’, ‘a’, ‘z’]

  3. Append can be used when we want to add something at the end of an list and it increases the length of the list by one.

    Extend can be used when we want to extend our already created list.The extend function iterates over each elements and adds it to the list.

  4. ->append adds its argument as a single element to the end of a list. The length of the list itself will increase by one.
    ->extend iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.
    append
    The list.append method appends an object to the end of the list.

    my_list.append(object)
    Whatever the object is, whether a number, a string, another list, or something else, it gets added onto the end of my_list as a single entry on the list.

    >>> my_list
    [‘f’, ‘b’]
    >>> my_list.append(‘z’)
    >>> my_list
    [‘f’, ‘b’, ‘z’]
    So keep in mind that a list is an object. If you append another list onto a list, the first list will be a single object at the end of the list (which may not be what you want):

    >>> another_list = [ 2, 3]
    >>> my_list.append(another_list)
    >>> my_list
    [‘f’, ‘b’, ‘z’, [ 2, 3]]
    #^^^^^^^^^— single item at the end of the list.
    extend
    The list.extend method extends a list by appending elements from an iterable:

    my_list.extend(iterable)
    So with extend, each element of the iterable gets appended onto the list. For example:

    >>> my_list
    [‘f’, ‘b’]
    >>> another_list = [1, 2, 3]
    >>> my_list.extend(another_list)
    >>> my_list
    [‘f’, ‘b’, 1, 2, 3]
    Keep in mind that a string is an iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string (which may not be what you want):

    >>> my_list.extend(‘baz’)
    >>> my_list
    [‘foo’, ‘bar’, 1, 2, 3, ‘b’, ‘a’, ‘z’]

  5. Append() : Adds its argument as a single element to the end of a list.
    The length of the list increases by one.
    Example :
    my_list = [‘The’, ‘Data’, ‘Monk’]
    another_list = [1, 2, 3, 4]
    my_list.append(another_list)
    print my_list
    #output
    [‘The’, ‘Data’, ‘Monk’, [1, 2, 3, 4]]

    Extend(): Iterates over its argument and adding each element to the list and extending the list.
    The length of the list increases by number of elements in it’s argument.

    Example:
    my_list = [‘Data’, ‘Monk’]
    another_list = [1, 2, 3, 4]
    my_list.extend(another_list)
    print my_list
    #output
    [‘Data’, ‘Monk’, 1, 2, 3, 4]

  6. In English append, the word means to add something at the end of the document and extend means to increase size/make it larger.

    similarly, the append function is used to add an element at the end of the list. It will increase the length of the list by 1.
    extend function is used to extend the already created list by adding more elements to it. Length of the list will depend on the number of elements added to it.


    Attachment

Leave an answer

Browse
Browse