Advertisement



< Prev
Next >



Python - List





In our previous Python article, we have explained a collection data type - tuple. Moving on in this tutorial, we are going to explain another collection data type provided by Python - list. Similar to a tuple, a list is also an ordered sequence of items. The items of a list are arranged in the order in which they are declared in the list, where the first item is stored at an index zero, the second item at index one and so on. Note : Each item is an object.





Using extend() method we could extend a list to contain new items or another list. This new item should be a iterable object i.e. a list or a tuple. The extend() method is called using a list object and it takes an iterable object as an argument. Let us see how we use extend() method.

#Python - Extending a list by calling extend() method


#First list
list1 =['A', -20]

#Second list
list2 =['have a good day', 2019]

#Printing lists
print('First List : ', list1)
print('Second List : ', list2)

#Calling extend() method to extend list1 to the include a list2 i.e. iterable object
list1.extend(list2)

#Printing new list
print('Updating First List : ',list1)

#Calling extend() method to extend list1 to contain multiple items of a list i.e. iterable object.
list1.extend([29,  'haha'])

#Printing new list
print('Updating First List : ',list1)


#Calling extend() method to extend list1 to contain a tuple  i.e. iterable object.
list1.extend((2.5,  1978))

#Printing new list
print('Updating First List : ',list1)

Output


First List :  ['A', -20]
Second List :  ['have a good day', 2019]
Updating First List :  ['A', -20, 'have a good day', 2019]
Updating First List :  ['A', -20, 'have a good day', 2019, 29, 'haha']
Updating First List :  ['A', -20, 'have a good day', 2019, 29, 'haha', 2.5, 1978]

In the program just above, we have created two list objects and then we have extended the first list to contain the elements of the second list, which results in a modified first list.

Note : Looking at the updated first list in output, you should understand that by calling the extend() method leads to just one list i.e. list is extended to contain the items of iterable object(be it a list or a tuple).




  • Some important built-in functions to work with list


  • Functions Description
    max() Returns the maximum value in a list.
    min() Returns the minimum value in a list.
    len() Returs the total length of a list.
    sum() Returns sum of all elements in a list.
    sorted() Returns a sorted copy of a list.
    any() Returns true if any element in a list is true, else false.
    all() Returns true if all elements in a list are true, else false.

    #Python - Functions working with lists 
    
    
    #First list
    list1 =[19, -20, 209, 799.99, -15.4]
    
    #Second list
    list2 =[1, True, False, 0] # 1 is boolean true, 0  for boolean false
                              # True(with capital T) is boolean true, False(with capital F) is boolean false
    
    #Printing lists
    print('List1 : ', list1)
    print('List2 : ', list2)
    
    
    
    #Using max() function with a list.
    print('Maximum value in List1 : ', max(list1))
    
    
    #Using min() function with a list.
    print('Minimum value in List1 : ', min(list1))
    
    
    #Using sum() function with a list.
    print('Sum of values in List1 : ', sum(list1))
    
    
    #Using sorted() function with a list.
    print('Sorted copy of List1 : ', sorted(list1))
    
    
    #Using all() function with a list.
    print('all() boolean true values in List2 : ', all(list2))
    
    
    #Using any() function with a list.
    print('any boolean true value in List2 : ', any(list2))


    Output


    List1 :  [19, -20, 209, 799.99, -15.4]
    List2 :  [1, True, False, 0]
    Maximum value in List1 :  799.99
    Minimum value in List1 :  -20
    Sum of values in List1 :  992.59
    Sorted copy of List1 :  [-20, -15.4, 19, 209, 799.99]
    all() boolean true values in List2 :  False
    any boolean true value in List2 :  True





    Please share this article -




    < Prev
    Next >
    < Python Tuple
    Python Set >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement