Search

Python Programming Filter

What are List Methods in Python

Sequence is one of the most basic data types in Python. Every element of a sequence is allocated a unique number called its position or index. The first designated index is zero, the second index is one, and so forth. Although Python comes with six types of pre-installed sequences, the most used ones are lists and tuples, and in this article we would be discussing about lists and its methods.Certain tasks can be performed with all types of sequence; these include adding, multiplying, indexing, slicing, and so on. For added convenience, Python provides built-in functions to find the length of a sequence and to find the largest and smallest elements of the sequence. If you are interested to learn more about other functions and features of Python, you may go through our Python tutorial.What is a ListList is the most versatile data-type available in Python that can be written as a collection of comma-separated values or items between square brackets. The items in a list need not necessarily be homogeneous, i.e. of the same type. This property of List makes it one of the most powerful tools in Python eg:[‘HELLO’, 57, “SKY”]. A single list can contain different Data-Types such as integers, strings, as well as Objects. Lists are mutable, and hence can be changed even after their creation.In Python, lists are ordered in a definite manner and have a definite count. Elements of a list are indexed according to a particular sequence and the indexing is done with 0 being the starting index. Even element of a list has its unique place in the list, which allows duplication of the elements in the list, with each element having its own distinct place and credibility. Lists are a useful tool for storing a sequence of data.Creating a list is as simple as putting different types of comma-separated values between square brackets:list1 = ['jack', 'jill', 1998, 2019];[Text Wrapping Break] list2 = [1, 2, 3, 4, 5 ];[Text Wrapping Break] list3 = ["w", "x", "y", "z"]Just like string indices, list indices start with 0, and lists can be sliced, concatenated and so on.Creating a ListPython Lists can be created just by placing the integer, string, or character inside the square brackets[]. Unlike Sets, a list does not require a built-in function for its creation.# Python program to demonstrate the creation of a list [Text Wrapping Break] # Creating a List [Text Wrapping Break] List = [] [Text Wrapping Break] print("Initial blank List: ") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # Creating a List with  [Text Wrapping Break] # the use of a String [Text Wrapping Break] List = ['PythonListDemo'] [Text Wrapping Break] print("\nList with the use of String: ") [Text Wrapping Break] print(List) [Text Wrapping Break][Text Wrapping Break] # Creating a List with [Text Wrapping Break] # the use of multiple values  [Text Wrapping Break] List = ["Python", "List", "Demo"] [Text Wrapping Break] print("\nList containing multiple values: ") [Text Wrapping Break] print(List[0]) [Text Wrapping Break] print(List[2]) [Text Wrapping Break] [Text Wrapping Break] # Creating a Multi-Dimensional List [Text Wrapping Break] # (By Nesting a list inside a List) [Text Wrapping Break] List = [['Python', 'List'] , ['Demo']] [Text Wrapping Break] print("\nMulti-Dimensional List: ") [Text Wrapping Break] print(List)Initial blank List: [Text Wrapping Break] [] [Text Wrapping Break] [Text Wrapping Break] List with the use of String: [Text Wrapping Break] ['PythonListDemo'] [Text Wrapping Break] [Text Wrapping Break] List containing multiple values: [Text Wrapping Break] Python [Text Wrapping Break] Demo[Text Wrapping Break] [Text Wrapping Break] Multi-Dimensional List:  [Text Wrapping Break] [['Python', 'List'], ['Demo']]Creating a list with multiple distinct or duplicate elements:Multiple distinct or duplicate values can be stored as a sequence during creation of list:# Creating a List with [Text Wrapping Break] # the use of  Numbers [Text Wrapping Break] # (Having duplicate values) [Text Wrapping Break] List = [1, 2, 4, 4, 3, 3, 3, 6, 5] [Text Wrapping Break] print("\nList with the use of Numbers: ")  [Text Wrapping Break] print(List) [Text Wrapping Break]  [Text Wrapping Break] # Creating a List with [Text Wrapping Break] # mixed type of values [Text Wrapping Break] #  (Having numbers and strings) [Text Wrapping Break] List = [1, 2, 'Python', 4, 'List', 6, 'Demo'] [Text Wrapping Break] print("\nList with the use of Mixed Values: ") [Text Wrapping Break] print(List)List with the use of Numbers: [Text Wrapping Break][1, 2, 4, 4, 3, 3, 3, 6,  5] [Text Wrapping Break] [Text Wrapping Break] List with the use of Mixed Values: [Text Wrapping Break] [1, 2, 'Python', 4, 'List', 6, 'Demo'] [Text Wrapping Break]Adding Elements to a ListUsing append() methodUsing the built-in append() function elements can be added to the List. At a time only one element can be added to the list by the use of append() method, but for the addition of multiple elements with the method, loops are used. Unlike Sets, a new List can be added to an existing one with the use of the append() method.# Python program to demonstrate addition of elements in a List [Text Wrapping Break] [Text Wrapping Break] # Creating a List [Text Wrapping Break] List = [] [Text Wrapping Break] print("Initial blank List: ") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] #  Addition of Elements  [Text Wrapping Break] # in the List [Text Wrapping Break] List.append(1) [Text Wrapping Break] List.append (2) [Text Wrapping Break] List.append(4) [Text Wrapping Break] print("\nList after Addition of Three elements: ") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break]# Adding elements to the List [Text Wrapping Break] # using Iterator [Text Wrapping Break] for i in range(1, 4): [Text Wrapping Break] List.append(i) [Text Wrapping Break] print("\nList after Addition of elements from 1-3: ") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # Addition of List to a List [Text Wrapping Break] List2 = ['Python', 'List'] [Text Wrapping Break] List.append(List2) [Text Wrapping Break] print ("\nList after Addition of a List: ") [Text Wrapping Break] print(List)Initial blank List: [Text Wrapping Break] [] [Text Wrapping Break]  [Text Wrapping Break] List after Addition of Three elements: [Text Wrapping Break] [1,2,4][Text Wrapping Break][Text Wrapping Break]List after Addition of elements from 1-3: [Text Wrapping Break] [1, 2, 4, 1, 2, 3] [Text Wrapping Break]  [Text Wrapping Break] List after Addition of a List:[Text Wrapping Break] [1, 2,  4, 1, 2, 3, ['Python', 'List']]Using insert() methodAppend() method restricts the addition of elements at the end of the List only. Using the insert() method, elements can be added to the list at your desired position. Unlike append() which requires only one argument, insert() method requires two arguments for defining the position and value of the element to be inserted (position, value).# Python program to demonstrate addition of elements in a List [Text Wrapping Break] [Text Wrapping Break] # Creating a List  [Text Wrapping Break] List = [1,2,3,4] [Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # Addition of Element at  [Text Wrapping Break] # specific Position [Text Wrapping Break] # (using Insert Method) [Text Wrapping Break] List.insert(3, 12) [Text Wrapping Break] List.insert(0, 'Python') [Text Wrapping Break] print("\nList after performing Insert Operation: ") [Text Wrapping Break] print(List)Initial List: [Text Wrapping Break] [1, 2, 3, 4] [Text Wrapping Break] [Text Wrapping Break] List after performing Insert Operation: [Text Wrapping Break] [ 'Python', 1, 2, 3, 12, 4]Using extend() methodApart from append() and insert() methods, there’s another method to add elements by the use of extend() method. This method is used for adding multiple elements to the end of the list at once.# Python program to demonstrate  [Text Wrapping Break] # Addition of elements in a List [Text Wrapping Break] [Text Wrapping Break] # Creating a List  [Text Wrapping Break] List = [1,2,3,4] [Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] #  Addition of multiple elements [Text Wrapping Break] # to the List at the end [Text Wrapping Break] # (using Extend Method)  [Text Wrapping Break] List.extend ( [8,  'Python', 'Program']) [Text Wrapping Break] print( "\nList after performing  Extend Operation: " ) [Text Wrapping Break] print(List)Initial List: [Text Wrapping Break] [1, 2, 3, 4] [Text Wrapping Break]  [Text Wrapping Break] List after performing Extend Operation:  [Text Wrapping Break] [1, 2, 3, 4, 8, 'Python', 'Program']Accessing elements from the ListIn order to access the items in a List, the index number is used as reference. The index operator [ ] is used to access the items of a list. The index should be an integer and nested lists are accessed by using nested indexing.# Python program to demonstrate  [Text Wrapping Break] # accessing of element from list [Text Wrapping Break][Text Wrapping Break] # Creating a List with [Text Wrapping Break]# the use of multiple values [Text Wrapping Break] List = ["Access", "List", "Elements"] [Text Wrapping Break]  [Text Wrapping Break]# accessing an element from the  [Text Wrapping Break] # list using index number [Text Wrapping Break] print("Accessing an element from the list") [Text Wrapping Break] print(List[0])  [Text Wrapping Break] print(List[2]) [Text Wrapping Break]  [Text Wrapping Break] # Creating a Multi-Dimensional List [Text Wrapping Break] # (By Nesting a list inside a List) [Text Wrapping Break] List = [['Access', 'List'] , ['Elements']] [Text Wrapping Break]  [Text Wrapping Break] # accessing an element from the [Text Wrapping Break] # Multi-Dimensional List using [Text Wrapping Break] # index number [Text Wrapping Break] print("Accessing an element from a Multi -  Dimensional list") [Text Wrapping Break] print(List[0][1]) [Text Wrapping Break] print(List[1][0])Accessing an element from the list [Text Wrapping Break] Access [Text Wrapping Break] Elements [Text Wrapping Break] [Text Wrapping Break] Accessing an element from a Multi-Dimensional  list [Text Wrapping Break] List [Text Wrapping Break] ElementsNegative indexingIn Python, negative sequence indexing means the representation of positions of the array from the end. Rather than calculating the offset like List[len(List)-3], we can just write it like List[-3]. Here, -1 refers to the last item, -2 refers to the second last item etc. i.e. beginning from the end.List = [1, 2, 'Python', 4, 'Negative', 6, 'Index'] [Text Wrapping Break] [Text Wrapping Break] # Accessing an element using negative indexing [Text Wrapping Break] print("Accessing element using negative indexing") [Text Wrapping Break]  [Text Wrapping Break] # print the last element of list [Text Wrapping Break] print(List[-1]) [Text Wrapping Break] [Text Wrapping Break]# print the third last element of list  [Text Wrapping Break] print(List[-3])Accessing element using negative indexing [Text Wrapping Break] Index [Text Wrapping Break] NegativeRemoving Elements from the ListUsing remove() methodIn Python, using the built-in remove() function, elements can be removed from the List but an Error will arise if the element is not present in the set. Remove() method is only capable of removing one element at a time, to remove a range of elements, an iterator is used. A limitation of this method is that it will only remove the first occurrence of the searched element and would not work if there are multiple occurrences of the searched element.# Python program to demonstrate removal of elements in a List [Text Wrapping Break][Text Wrapping Break] # Creating a List [Text Wrapping Break] List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List) [Text Wrapping Break]  [Text Wrapping Break] # Removing elements from List [Text Wrapping Break] # using Remove() method [Text Wrapping Break] List.remove(5) [Text Wrapping Break] List.remove(6) [Text Wrapping Break] print("\nList after removal of two elements: ") [Text Wrapping Break] print(List) [Text Wrapping Break]  [Text Wrapping Break] # Removing elements from List [Text Wrapping Break] # using iterator method [Text Wrapping Break] for i in range(1, 5): [Text Wrapping Break]    List.remove(i) [Text Wrapping Break] print("\nList after removing a range of elements: ") [Text Wrapping Break] print(List)Initial List: [Text Wrapping Break][1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [Text Wrapping Break][Text Wrapping Break]List after removal of two elements: [Text Wrapping Break][1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12][Text Wrapping Break] [Text Wrapping Break] List after removing a range of elements: [Text Wrapping Break] [7, 8, 9, 10, 11, 12]Using pop() methodIn Python,  we can also remove and return an element from the set using the Pop() function, but it removes  the last element of the set only by default. To remove a specific element from a position of the List, index of the element is passed as an argument to the pop() function.List = [1,2,3,4,5] [Text Wrapping Break] [Text Wrapping Break] # Removing element from the  [Text Wrapping Break] # Set using the pop() method [Text Wrapping Break] List.pop() [Text Wrapping Break] print("\nList after popping an element: ") [Text Wrapping Break] print(List) [Text Wrapping Break]  [Text Wrapping Break] # Removing element at a  [Text Wrapping Break] # specific location from the  [Text Wrapping Break] # Set using the pop() method [Text Wrapping Break] List.pop(2) [Text Wrapping Break] print("\nList after popping a specific element: ") [Text Wrapping Break] print(List)List after popping an element: [Text Wrapping Break] [1, 2, 3, 4]  [Text Wrapping Break][Text Wrapping Break]List after popping a specific  element: [Text Wrapping Break][1, 2, 4]Slicing of a ListAlthough there are several ways to print the whole List with all the elements in Python, there is only one way to print a specific range of elements from the list: by the use of Slice operation. Slice operation is performed on Lists by the use of colon(:). For printing elements from the beginning of the range use [:Index], for printing elements from end use [:-Index], to print elements from a specific index till the end use [Index:], for printing elements within a specific range, use [Start Index: End Index] and to print the entire List by the use of slicing operation, use [:]. Moreover, in order to print entire List in reverse order, use [::-1]. For printing the elements of List from rear end, negative indexes are used.# Python program to demonstrate removal of elements in a List [Text Wrapping Break][Text Wrapping Break]# Creating a List [Text Wrapping Break] List = ['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'] [Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List) [Text Wrapping Break]  [Text Wrapping Break] # Print elements of a range [Text Wrapping Break] # using Slice operation [Text Wrapping Break] Sliced_List = List[3:10] [Text Wrapping Break] print("\nSlicing elements in a range 3-10: ") [Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] # Print elements from a  [Text Wrapping Break] # pre-defined point to end [Text Wrapping Break] Sliced_List = List[6:] [Text Wrapping Break] print("\nElements sliced from 6th ""element till the end: ") [Text Wrapping Break] print(Sliced_List) [Text Wrapping Break] [Text Wrapping Break]# Printing elements from [Text Wrapping Break] # beginning till end [Text Wrapping Break] Sliced_List = List[:] [Text Wrapping Break] print("\nPrinting all elements using slice operation: ") [Text Wrapping Break] print(Sliced_List) Initial List: [Text Wrapping Break] ['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'] [Text Wrapping Break] [Text Wrapping Break] Slicing elements in a range 3-10: [Text Wrapping Break]['H', 'O', 'N', 'P', 'R','O','G'][Text Wrapping Break][Text Wrapping Break]Elements sliced from 6th element till the end: [Text Wrapping Break]['P', 'R', 'O', 'G', 'R', 'A', 'M'][Text Wrapping Break][Text Wrapping Break]Printing all elements using slice operation: [Text Wrapping Break] ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']Negative index List Slicing# Creating a List [Text Wrapping Break] List = ['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'] [Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List) [Text Wrapping Break]  [Text Wrapping Break] # Print elements from beginning [Text Wrapping Break] # to a pre-defined point using Slice [Text Wrapping Break] Sliced_List = List[:-7] [Text Wrapping Break] print("\nElements sliced till 7th element from last: ") [Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #  Print elements of a range [Text Wrapping Break] # using negative index List slicing [Text Wrapping Break] Sliced_List = List[-6:-1] [Text Wrapping Break] print("\nElements sliced from index -6 to -1") [Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #  Printing elements in reverse [Text Wrapping Break] # using Slice operation [Text Wrapping Break] Sliced_List = List[::-1] [Text Wrapping Break] print("\nPrinting List in reverse: ") [Text Wrapping Break] print(Sliced_List)Initial List: [Text Wrapping Break] ['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'][Text Wrapping Break] [Text Wrapping Break]Elements sliced till 7th element from last: [Text Wrapping Break]['P', 'Y', 'T', 'H', 'O', 'N'][Text Wrapping Break] [Text Wrapping Break]Elements sliced from index -6 to -1[Text Wrapping Break] ['R', 'O', 'G', 'R', 'A'][Text Wrapping Break] [Text Wrapping Break]Printing List in reverse:[Text Wrapping Break]['M', 'A', 'R', 'G', 'O', 'R', 'P', 'N', 'O', 'H', 'T', 'Y', 'P']Updating ListsYou can update single or multiple elements of lists by reassigning the values on the lists individually.# Python program to update elements of a list[Text Wrapping Break]list = ['physics', 'chemistry', 1998, 2019];[Text Wrapping Break] print "Value available at index 2 : "[Text Wrapping Break] print list[2] [Text Wrapping Break] list[2] = 2000;[Text Wrapping Break] print "New value available at index 2 : "[Text Wrapping Break] print list[2]Value available at index 2 : [Text Wrapping Break] 1998 [Text Wrapping Break] New value available at index 2 : [Text Wrapping Break] 2000Built-in functionsFUNCTION DESCRIPTIONsum() Addsall numbers.ord() Used for returning an integer which represents the unique Unicode code point of the given Unicode character.cmp() If the first list is “greater” than the second list, the function returns 1.max() It returns the largest element in the list.min() It returns the smallest element in the list.all() It returns true if all elements are true or false if any element in the list is empty.any() It returns true if even one of the elements of the list is true. If one list is empty, it returns false.len() It returns length the list.enumerate() It adds a counter to an enumerate object that can be used directly for loops.accumulate() It makes an iterator that gives the result of a function. It takes a function as an argument.filter() It can individually check if every element is true or not.map() It applies a particular function to each item of an iterable and shows a list of the results.lambda() It is an anonymous function that behaves like a normal function in regard to arguments. While normal functions are defined with def keyword, anonymous functions are defined using lambda keyword.List MethodsFUNCTION DESCRIPTIONAppend() Adds an element at the end of the listExtend() Adds all elements of one list to another listInsert() Inserts an item at a desired indexRemove() Removes an item from the listPop() Removes and returns an element at a desired indexClear() Removes all elements from the listIndex() Returns the index of the first identical itemCount() Returns the number of items passed as argumentSort() Sort items of a list in ascending orderReverse() Reverses the listcopy() Returns a copy of the listSummaryIn this article, we have covered the concept of Lists in Python. You have learned the basics of creating a List, adding value to it, accessing its elements, removing the elements, and various other operations. We have also covered some basic built-in functions of Python and several other methods along with their functions. To gain more knowledge about Python tips and tricks, check our Python tutorial and get a good hold over coding in Python by joining the Python certification course.
Rated 4.5/5 based on 12 customer reviews

What are List Methods in Python

5059
What are List Methods in Python

Sequence is one of the most basic data types in Python. Every element of a sequence is allocated a unique number called its position or index. The first designated index is zero, the second index is one, and so forth. Although Python comes with six types of pre-installed sequences, the most used ones are lists and tuples, and in this article we would be discussing about lists and its methods.

Certain tasks can be performed with all types of sequence; these include adding, multiplying, indexing, slicing, and so on. For added convenience, Python provides built-in functions to find the length of a sequence and to find the largest and smallest elements of the sequence. If you are interested to learn more about other functions and features of Python, you may go through our Python tutorial.

What is a List

List is the most versatile data-type available in Python that can be written as a collection of comma-separated values or items between square brackets. The items in a list need not necessarily be homogeneous, i.e. of the same type. This property of List makes it one of the most powerful tools in Python eg:[‘HELLO’, 57, “SKY”]. A single list can contain different Data-Types such as integers, strings, as well as Objects. Lists are mutable, and hence can be changed even after their creation.

In Python, lists are ordered in a definite manner and have a definite count. Elements of a list are indexed according to a particular sequence and the indexing is done with 0 being the starting index. Even element of a list has its unique place in the list, which allows duplication of the elements in the list, with each element having its own distinct place and credibility. Lists are a useful tool for storing a sequence of data.

Creating a list is as simple as putting different types of comma-separated values between square brackets:

list1 = ['jack', 'jill', 1998, 2019];[Text Wrapping Break] list2 = [1, 2,
 3, 4, 5 ];[Text Wrapping Break] list3 = ["w", "x", "y", "z"]

Just like string indices, list indices start with 0, and lists can be sliced, concatenated and so on.

Creating a List

Python Lists can be created just by placing the integer, string, or character inside the square brackets[]. Unlike Sets, a list does not require a built-in function for its creation.

# Python program to demonstrate the creation of a list
[Text Wrapping Break] # Creating a List [Text Wrapping Break] List = []
[Text Wrapping Break] print("Initial blank List: ")
[Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] #
Creating a List with  [Text Wrapping Break] # the use of a String
[Text Wrapping Break] List = ['PythonListDemo']
[Text Wrapping Break] print("\nList with the use of String: ")
[Text Wrapping Break] print(List) [Text Wrapping Break][Text Wrapping Break] # 
Creating a List with [Text Wrapping Break] # the use of multiple values 
[Text Wrapping Break] List = ["Python""List""Demo"]
[Text Wrapping Break] print("\nList containing multiple values: ")
[Text Wrapping Break] print(List[0]) [Text Wrapping Break] print(List[2])
[Text Wrapping Break] [Text Wrapping Break] # Creating a Multi-Dimensional List
[Text Wrapping Break] # (By Nesting a list inside a List) [Text Wrapping Break] List = [['Python', 'List'] , ['Demo']]
[Text Wrapping Break] print("\nMulti-Dimensional List: ")
[Text Wrapping Break] print(List)
Initial blank List:
[Text Wrapping Break]
[] [Text Wrapping Break] [Text Wrapping Break]
List with the use of String:
[Text Wrapping Break]
['PythonListDemo'] [Text Wrapping Break]
[Text Wrapping Break] List 
containing multiple values:
[Text Wrapping Break] Python [Text Wrapping Break]
Demo[Text Wrapping Break] [Text Wrapping Break]
Multi-Dimensional List: 
[Text Wrapping Break] [['Python',
'List'], ['Demo']]

Creating a list with multiple distinct or duplicate elements:

Multiple distinct or duplicate values can be stored as a sequence during creation of list:

# Creating a List with [Text Wrapping Break] # the use of 
Numbers [Text Wrapping Break] # (Having duplicate values)
[Text Wrapping Break] List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
[Text Wrapping Break] print("\nList with the use of Numbers: "[Text Wrapping Break] print(List) [Text Wrapping Break] 
[Text Wrapping Break] # Creating a List with
[Text Wrapping Break] # mixed type of values [Text Wrapping Break] # 
(Having numbers and strings) [Text Wrapping Break] List = [1,
2, 'Python', 4, 'List', 6, 'Demo']
[Text Wrapping Break] print("\nList with the use of Mixed
Values: ") [Text Wrapping Break] print(List)
List with the use of Numbers: [Text Wrapping Break][1, 2, 4, 4, 3, 3, 3, 6, 
5] [Text Wrapping Break] [Text Wrapping Break] List with the use of Mixed Values:
[Text Wrapping Break] [1, 2, 'Python', 4, 'List', 6, 'Demo'] [Text Wrapping Break]

Adding Elements to a List

Using append() method

Using the built-in append() function elements can be added to the List. At a time only one element can be added to the list by the use of append() method, but for the addition of multiple elements with the method, loops are used. Unlike Sets, a new List can be added to an existing one with the use of the append() method.

# Python program to demonstrate addition of elements in a List
[Text Wrapping Break] [Text Wrapping Break] # Creating a List
[Text Wrapping Break] List = [] [Text Wrapping Break] print("Initial blank List: ")
[Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # 
Addition of Elements  [Text Wrapping Break] # in the List
[Text Wrapping Break] List.append(1) [Text Wrapping Break] List.append (2)
[Text Wrapping Break] List.append(4) [Text Wrapping Break] print("\nList after
Addition of Three elements: ") [Text Wrapping Break] print(List)
[Text Wrapping Break] [Text Wrapping Break]# Adding elements to the List
[Text Wrapping Break] # using Iterator [Text Wrapping Break] for i in range(1, 4):
[Text Wrapping Break] List.append(i) [Text Wrapping Break] print("\nList after
Addition of elements from 1-3: ") [Text Wrapping Break] print(List)
[Text Wrapping Break] [Text Wrapping Break] # Addition of List to a List
[Text Wrapping Break] List2 = ['Python', 'List']
[Text Wrapping Break] List.append(List2) [Text Wrapping Break] print ("\nList after
Addition of a List: ") [Text Wrapping Break] print(List)
Initial blank List: [Text Wrapping Break] [] [Text Wrapping Break] 
[Text Wrapping Break] List after Addition of Three elements: [Text Wrapping Break]
[1,2,4][Text Wrapping Break][Text Wrapping Break]List after Addition of
elements from 1-3[Text Wrapping Break] [1, 2, 4, 1, 2, 3[Text Wrapping Break] 
[Text Wrapping Break] List after Addition of a List:[Text Wrapping Break] [1, 2, 
4, 1, 2, 3, ['Python', 'List']]

Using insert() method

Append() method restricts the addition of elements at the end of the List only. Using the insert() method, elements can be added to the list at your desired position. Unlike append() which requires only one argument, insert() method requires two arguments for defining the position and value of the element to be inserted (position, value).

# Python program to demonstrate addition of elements in a List
[Text Wrapping Break] [Text Wrapping Break] # Creating a List 
[Text Wrapping Break] List = [1,2,3,4] [Text Wrapping Break] print("Initial List: 
") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] #
Addition of Element at  [Text Wrapping Break] # specific Position
[Text Wrapping Break] # (using Insert Method) [Text Wrapping Break] List.insert(3,
12) [Text Wrapping Break] List.insert(0, 'Python')
[Text Wrapping Break] print("\nList after performing Insert Operation: ")
[Text Wrapping Break] print(List)
Initial List: [Text Wrapping Break] [1, 2, 3, 4] [Text Wrapping Break]
[Text Wrapping Break] List after performing Insert Operation:
[Text Wrapping Break] [ 'Python', 1, 2, 3, 12, 4]

Using extend() method

Apart from append() and insert() methods, there’s another method to add elements by the use of extend() method. This method is used for adding multiple elements to the end of the list at once.

# Python program to demonstrate  [Text Wrapping Break] # Addition of elements
in a List [Text Wrapping Break] [Text Wrapping Break] # Creating a List 
[Text Wrapping Break] List = [1,2,3,4] [Text Wrapping Break] print("Initial List:
") [Text Wrapping Break] print(List) [Text Wrapping Break] [Text Wrapping Break] # 
Addition of multiple elements [Text Wrapping Break] # to the List at the end
[Text Wrapping Break] # (using Extend Method) 
[Text Wrapping Break] List.extend ( [8,
 'Python', 'Program']) [Text Wrapping Break] print( "\nList after performing 
Extend Operation: " ) [Text Wrapping Break] print(List)
Initial List: [Text Wrapping Break] [1, 2, 3, 4] [Text Wrapping Break] 
[Text Wrapping Break] List after performing Extend Operation: 
[Text Wrapping Break] [1, 2, 3, 4, 8, 'Python', 'Program']

Accessing elements from the List

In order to access the items in a List, the index number is used as reference. The index operator [ ] is used to access the items of a list. The index should be an integer and nested lists are accessed by using nested indexing.

# Python program to demonstrate  [Text Wrapping Break] # accessing of element
from list [Text Wrapping Break][Text Wrapping Break] # Creating a List with
[Text Wrapping Break]# the use of multiple values [Text Wrapping Break] List =
["Access", "List", "Elements"] [Text Wrapping Break]  [Text Wrapping Break]#
accessing an element from the  [Text Wrapping Break] # list using index number
[Text Wrapping Break] print("Accessing an element from the list")
[Text Wrapping Break] print(List[0])  [Text Wrapping Break] print(List[2])
[Text Wrapping Break]  [Text Wrapping Break] # Creating a Multi-Dimensional List
[Text Wrapping Break] # (By Nesting a list inside a List)
[Text Wrapping Break] List = [['Access', 'List'] , ['Elements']]
[Text Wrapping Break]  [Text Wrapping Break] # accessing an element from the
[Text Wrapping Break] # Multi-Dimensional List using [Text Wrapping Break] # index
number [Text Wrapping Break] print("Accessing an element from a Multi - 
Dimensional list") [Text Wrapping Break] print(List[0][1])
[Text Wrapping Break] print(List[1][0])
Accessing an element from the
list [Text Wrapping Break] Access [Text Wrapping Break] Elements [Text Wrapping Break] [Text Wrapping Break]
Accessing an element from a Multi-Dimensional
 list [Text Wrapping Break] List [Text Wrapping Break] Elements

Negative indexing

In Python, negative sequence indexing means the representation of positions of the array from the end. Rather than calculating the offset like List[len(List)-3], we can just write it like List[-3]. Here, -1 refers to the last item, -2 refers to the second last item etc. i.e. beginning from the end.

List = [1, 2, 'Python', 4, 'Negative', 6, 'Index'] [Text Wrapping Break]
[Text Wrapping Break] # Accessing an element using negative indexing
[Text Wrapping Break] print("Accessing element using negative indexing")
[Text Wrapping Break]  [Text Wrapping Break] # print the last element of list
[Text Wrapping Break] print(List[-1]) [Text Wrapping Break] [Text Wrapping Break]#
print the third last element of list  [Text Wrapping Break] print(List[-3])
Accessing element using negative
indexing [Text Wrapping Break] Index [Text Wrapping Break] Negative

Removing Elements from the List

Using remove() method

In Python, using the built-in remove() function, elements can be removed from the List but an Error will arise if the element is not present in the set. Remove() method is only capable of removing one element at a time, to remove a range of elements, an iterator is used. A limitation of this method is that it will only remove the first occurrence of the searched element and would not work if there are multiple occurrences of the searched element.

# Python program to demonstrate removal of elements in a List
[Text Wrapping Break][Text Wrapping Break] # Creating a List
[Text Wrapping Break] List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Removing elements from List
[Text Wrapping Break] # using Remove() method [Text Wrapping Break] List.remove(5)
[Text Wrapping Break] List.remove(6) [Text Wrapping Break] print("\nList after
removal of two elements: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Removing elements from List
[Text Wrapping Break] # using iterator method [Text Wrapping Break] for i in
range(1, 5): [Text Wrapping Break]    List.remove(i)
[Text Wrapping Break] print("\nList after removing a range of elements: ")
[Text Wrapping Break] print(List)
Initial List: [Text Wrapping Break][1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[Text Wrapping Break][Text Wrapping Break]List after removal of two elements:
[Text Wrapping Break][1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12][Text Wrapping Break]
[Text Wrapping Break] List after removing a range of elements:
[Text Wrapping Break] [7, 8, 9, 10, 11, 12]

Using pop() method

In Python,  we can also remove and return an element from the set using the Pop() function, but it removes  the last element of the set only by default. To remove a specific element from a position of the List, index of the element is passed as an argument to the pop() function.

List = [1,2,3,4,5] [Text Wrapping Break] [Text Wrapping Break] # Removing element
from the  [Text Wrapping Break] # Set using the pop() method
[Text Wrapping Break] List.pop() [Text Wrapping Break] print("\nList after popping
an element: ") [Text Wrapping Break] print(List) [Text Wrapping Break] 
[Text Wrapping Break] # Removing element at a  [Text Wrapping Break] # specific
location from the  [Text Wrapping Break] # Set using the pop() method
[Text Wrapping Break] List.pop(2) [Text Wrapping Break] print("\nList after popping
a specific element: ") [Text Wrapping Break] print(List)
List after popping an element: [Text Wrapping Break] [1, 2, 3, 4[Text Wrapping Break][Text Wrapping Break]List after popping a specific 
element: [Text Wrapping Break][1, 2, 4]

Slicing of a List

Although there are several ways to print the whole List with all the elements in Python, there is only one way to print a specific range of elements from the list: by the use of Slice operation. Slice operation is performed on Lists by the use of colon(:). For printing elements from the beginning of the range use [:Index], for printing elements from end use [:-Index], to print elements from a specific index till the end use [Index:], for printing elements within a specific range, use [Start Index: End Index] and to print the entire List by the use of slicing operation, use [:]. Moreover, in order to print entire List in reverse order, use [::-1]. For printing the elements of List from rear end, negative indexes are used.

# Python program to demonstrate removal of elements in a List
[Text Wrapping Break][Text Wrapping Break]# Creating a List
[Text Wrapping Break] List =
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']
[Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Print elements of a range
[Text Wrapping Break] # using Slice operation [Text Wrapping Break] Sliced_List =
List[3:10] [Text Wrapping Break] print("\nSlicing elements in a range 3-10: ")
[Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #
Print elements from a  [Text Wrapping Break] # pre-defined point to end
[Text Wrapping Break] Sliced_List = List[6:]
[Text Wrapping Break] print("\nElements sliced from 6th ""element till the
end: ") [Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]
[Text Wrapping Break]# Printing elements from [Text Wrapping Break] # beginning
till end [Text Wrapping Break] Sliced_List = List[:]
[Text Wrapping Break] print("\nPrinting all elements using slice operation: ")
[Text Wrapping Break] print(Sliced_List) 
Initial List: [Text Wrapping Break]
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'[Text Wrapping Break]
[Text Wrapping Break] Slicing elements in a range 3-10: [Text Wrapping Break]['H',
'O', 'N', 'P', 'R','O','G'][Text Wrapping Break][Text Wrapping Break]Elements
sliced from 6th element till the end: [Text Wrapping Break]['P', 'R', 'O',
'G', 'R', 'A', 'M'][Text Wrapping Break][Text Wrapping Break]Printing all
elements using slice operation: [Text Wrapping Break] ['P', 'Y', 'T', 'H',
'O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']

Negative index List Slicing

# Creating a List [Text Wrapping Break] List =
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M']
[Text Wrapping Break] print("Initial List: ") [Text Wrapping Break] print(List)
[Text Wrapping Break]  [Text Wrapping Break] # Print elements from beginning
[Text Wrapping Break] # to a pre-defined point using Slice
[Text Wrapping Break] Sliced_List = List[:-7]
[Text Wrapping Break] print("\nElements sliced till 7th element from last: ")
[Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #
 Print elements of a range [Text Wrapping Break] # using negative index List
slicing [Text Wrapping Break] Sliced_List = List[-6:-1]
[Text Wrapping Break] print("\nElements sliced from index -6 to -1")
[Text Wrapping Break] print(Sliced_List) [Text Wrapping Break]  [Text Wrapping Break] #
 Printing elements in reverse [Text Wrapping Break] # using Slice operation
[Text Wrapping Break] Sliced_List = List[::-1]
[Text Wrapping Break] print("\nPrinting List in reverse: ")
[Text Wrapping Break] print(Sliced_List)
Initial List: [Text Wrapping Break]
['P', 'Y', 'T', 'H','O', 'N', 'P', 'R', 'O', 'G', 'R', 'A', 'M'][Text Wrapping Break]
[Text Wrapping Break]Elements sliced till 7th element from last:
[Text Wrapping Break]['P', 'Y', 'T', 'H', 'O', 'N'][Text Wrapping Break]
[Text Wrapping Break]Elements sliced from index -6 to -1[Text Wrapping Break]
['R', 'O', 'G', 'R', 'A'][Text Wrapping Break] [Text Wrapping Break]Printing List
in reverse:[Text Wrapping Break]['M', 'A', 'R', 'G', 'O', 'R', 'P', 'N',
'O', 'H', 'T', 'Y', 'P']

Updating Lists

You can update single or multiple elements of lists by reassigning the values on the lists individually.

# Python program to update elements of a list[Text Wrapping Break]list =
['physics', 'chemistry', 1998, 2019];[Text Wrapping Break] print "Value
available at index 2 : "[Text Wrapping Break] print list[2]
[Text Wrapping Break] list[2] = 2000;[Text Wrapping Break] print "New value
available at index 2 : "[Text Wrapping Break] print list[2]
Value available at index 2 : [Text Wrapping Break] 1998 [Text Wrapping Break] New
value available at index 2 : [Text Wrapping Break] 2000

Built-in functions

FUNCTION DESCRIPTION

sum() Addsall numbers.

ord() Used for returning an integer which represents the unique Unicode code point of the given Unicode character.

cmp() If the first list is “greater” than the second list, the function returns 1.

max() It returns the largest element in the list.

min() It returns the smallest element in the list.

all() It returns true if all elements are true or false if any element in the list is empty.

any() It returns true if even one of the elements of the list is true. If one list is empty, it returns false.

len() It returns length the list.

enumerate() It adds a counter to an enumerate object that can be used directly for loops.

accumulate() It makes an iterator that gives the result of a function. It takes a function as an argument.

filter() It can individually check if every element is true or not.

map() It applies a particular function to each item of an iterable and shows a list of the results.

lambda() It is an anonymous function that behaves like a normal function in regard to arguments. While normal functions are defined with def keyword, anonymous functions are defined using lambda keyword.

List Methods

FUNCTION DESCRIPTION

Append() Adds an element at the end of the list

Extend() Adds all elements of one list to another list

Insert() Inserts an item at a desired index

Remove() Removes an item from the list

Pop() Removes and returns an element at a desired index

Clear() Removes all elements from the list

Index() Returns the index of the first identical item

Count() Returns the number of items passed as argument

Sort() Sort items of a list in ascending order

Reverse() Reverses the list

copy() Returns a copy of the list

Summary

In this article, we have covered the concept of Lists in Python. You have learned the basics of creating a List, adding value to it, accessing its elements, removing the elements, and various other operations. We have also covered some basic built-in functions of Python and several other methods along with their functions. To gain more knowledge about Python tips and tricks, check our Python tutorial and get a good hold over coding in Python by joining the Python certification course.

Priyankur

Priyankur Sarkar

Data Science Enthusiast

Priyankur Sarkar loves to play with data and get insightful results out of it, then turn those data insights and results in business growth. He is an electronics engineer with a versatile experience as an individual contributor and leading teams, and has actively worked towards building Machine Learning capabilities for organizations.

Join the Discussion

Your email address will not be published. Required fields are marked *

Suggested Blogs

Scala Vs Python Vs R Vs Java - Which language is better for Spark & Why?

One of the most important decisions for the Big data learners or beginners is choosing the best programming language for big data manipulation and analysis. Just understanding business problems and choosing the right model is not enough but implementing them perfectly is equally important and choosing the right language (or languages) for solving the problem goes a long way. If you search top and highly effective programming languages for Big Data on Google, you will find the following top 4 programming languages: JavaScalaPythonRJavaJava is one of the oldest languages of all 4 programming languages listed here. Traditional Frameworks of Big data like Apache Hadoop and all the tools within its ecosystem are Java-based and hence using java opens up the possibility of utilizing large ecosystem of tools in the big data world.  ScalaA beautiful crossover between object-oriented and functional programming language is Scala. Scala is a highly Scalable Language. Scala was invented by the German Computer Scientist, Martin Odersky and the first version was launched in the year 2003.PythonPython was originally conceptualized by Guido van Rossum in the late 1980s. Initially, it was designed as a response to the ABC programming language and later gained its popularity as a functional language in a big data world. Python has been declared as one of the fastest-growing programming languages in 2018 as per the recently held Stack Overflow Developer Survey. Many data analysis, manipulation, machine learning, deep learning libraries are written in Python and hence it has gained its popularity in the big data ecosystem. It’s a very user-friendly language and it is its biggest advantage.  Fun factPython is not named after the snake. It’s named after the British TV show Monty Python.RR is the language of statistics. R is a language and environment for statistical computing and graphics. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is named partly after the first names of the first two R authors and partly as a play on the name of S*. The project was conceived in 1992, with an initial version released in 1995 and a stable beta version in 2000.*SS is a statistical programming language developed primarily by John Chambers and R is an implementation of the S programming language combined with lexical scoping semantics, inspired by Scheme.Every framework is implemented in the underlying programming language for its implementation. Ex Zend uses PHP, Panda Framework uses python similarly Hadoop framework uses Java and Spark uses Scala.However, Spark officially supports Java, Scala, Python and R, all 4 languages. If one browses through Apache Spark’s official website documentation, he/she would find many other languages utilized by the open-source community for Spark implementation.    When any developer wants to start learning Spark, the first question he stumbles upon is, out of these pools of languages, which one to use and which one to master? Solution Architects would have a tough time choosing the right language for spark framework and Organizations will always be wondering, which skill sets are relevant for my problem if one doesn’t have the right knowledge about these languages in the context of Spark.    This article will try to answer all these queries.so let’s start-JavaOldest of all and popular, widely adopted programming language of all. There is a number offeatures/advantages due to which Java is favorite for Big data developers and tool creators:Java is platform-agnostic language and hence it can run on almost any system. Java is portable due to something called Java Virtual Machine – JVM. JVM is a foundation of Hadoop ecosystem tools like Map Reduce, Storm, Spark, etc. These tools are written in Java and run on JVM.Java provides various communities support like GitHub and stack overflow etc.Java is scalable, backward compatible, stable and production-ready language. Also, supports a large variety of tried and tested libraries.It is statically typed language (We would see details of this functionality in later sections, in comparison with others)Java is mostly the choice for most of the big data projects but for the Spark framework, one has to ponder upon, whether Java would be the best fit.One major drawback of Java is its verbosity. One has to write long code (number of lines of code) to achieve simple functionality in Java.Java does not support Read-Evaluate-Print-Loop (REPL) which is a major deal-breaker when choosing a programming language for big data processing.ScalaScala is comparatively new to the programming scene but has become popular very quickly. Above are a few quotes from bigger names in the industry for Scala. From the Spark context, many experts prefer Scala over other programming languages as Spark is written in Scala. Scala is the native language of Spark. It means any new API always first be available in Scala.Scala is a hybrid functional programming language because It has both the features of object-oriented programming and functional programming. As an OO Programming Language, it considers every value as an object and all OOPS concepts apply. As a functional programming language, it defines and supports functions. All operations are done as functions. No variable stands by itself. Scala is a machine-compiled language.Scala and Java are popular programming languages that run over JVM. JVM makes these languages framework friendly. One can say, Scala is an advanced level of Java.Features/Advantages of Scala:It’s general-purpose object-oriented language with functional language properties too. It’s less verbose than Java.It can work with JVM and hence is portable.It can support Java APIs comfortably.It's fast and robust in Spark context as its Spark native.It is a statically typed language.Scala supports Read-Evaluate-Print-Loop (REPL)Drawbacks / Downsides of Scala:Scala is complex to learn due to the functional nature of language.Steep learning curve.Lack of matured machine learning languages.PythonPython is one of the de-facto languages of Data Science. It is a simple, open-source, general-purpose language and is very easy to learn. It has a rich set of libraries, utilities, ready-to-use features and support to a number of mature machine learning, big data processing, visualization libraries.Advantages of Python:It is interpreted language (i.e. support to REPL, Read, Evaluate, Print, Loop.) If you type a command into a command-line interpreter and it responds immediately. Java lacks this feature.Easy to learn, easy debugging, fewer lines of code.It is dynamically typed. i.e. can dynamically defined variable types. i.e. Python as a language is type-safe.Python is platform agnostic and scalable.Drawbacks/Disadvantages:Python is slow. Big data professionals find projects built in Java / Scala are faster and robust than the once with python.Whilst using user-defined functions or third party libraries in Python with Spark, processing would be slower as increased processing is involved as Python does not have equivalent Java/Scala native language API for these functionalities.Python does not support heavy weight processing fork() using uWSGI but it does not support true multithreading.R LanguageR is the favourite language of statisticians. R is fondly called a language of statisticians.  It’s popular for research, plotting, and data analysis. Together with RStudio, it makes a killer statistic, plotting, and data analytics application.R is majorly used for building data models to be used for data analysis.Advantages/Features of R:Strong statistical modeling and visualization capabilities.Support for ‘data science’ related work.It can be integrated with Apache Hadoop and Spark easily.Drawbacks/Disadvantages of R:R is not a general-purpose language.The code written in R cannot be directly deployed into production. It needs conversion into Java or Python.Not as fast as Java / Scala.Comparison of four languages for Apache SparkWith the introduction of these 4 languages, let’s now compare these languages for the Spark framework:These languages can be categorized into 2 buckets basis high-level spark architecture support, broadly:JVM Languages: Java and ScalaNon-JVM Languages: Python and RDue to these categorizations, performance may vary. Let’s understand architecture in little depth to understand the performance implications of using these languages. This would also help us to understand the question of when to use which language.Spark Framework High-level architecture An application written in any one of the languages is submitted on the driver node and further driver node distributes the workload by dividing the execution on multiple worker nodes.JVM compatible Application Execution Flow Consider the applications written are JVM compatible (Java/Scala). Now, Spark is also written in native JVM compatible Scala language, hence there is no explicit conversion required at any point of time to execute JVM compatible applications on Spark. Also, this makes the native language applications faster to perform on the Spark framework.There are multiple scenarios for Python/R written applications:Python/R driver talk to JVM driver by socket-based API. On the driver node, both the driver processes are invoked when the application language is non-JVM language.Scenario 1: Applications for which Equivalent Java/Scala Driver API exists - This scenario executes the same way as JVM compatible applications by invoking Java API on the driver node itself. The cost for inter-process communication through sockets is negligible and hence performance is comparable. This is with the assumption that processed data over worker nodes are not to be sent back to the Driver again.Scenario 1(b): If the assumption taken is void in scenario 1 i.e. processed data on worker nodes is to be sent back to driver then there is significant overhead and serialization required. This adds to processing time and hence performance in this scenario deteriorates.Scenario 2: Applications for which Equivalent Java/Scala Driver API do not exist – Ex. UDF (User-defined functions) / Third party python libraries. In such cases equivalent Java API doesn’t exist and hence, additional executor sessions are initiated on worker node and python API is serialized on worker node and executed. This python worker processes in addition to JVM and coordination between them is overhead. Processes also compete for resources which adds to memory contention.In addition, if the data is to send back to the driver node then processing takes a lot of time and problem scales up as volume increases and hence performance is bigger problem.As we have seen a performance, Let’s see the tabular comparison between these languages.Comparison PointsJavaScalaPythonRPerformanceFasterFaster (about 10x faster than Python)SlowerSlowerLearning CurveEasier than JavaTougher than PythonSteep learning curve than Java & PythonEasiestModerateUser GroupsWeb/Hadoop programmersBig Data ProgrammersBeginners & Data EngineersData Scientists/ StatisticiansUsageWeb development and Hadoop NativeSpark NativeData Engineering/ Machine Learning/ Data VisualizationVisualization/ Data Analysis/ Statistics use casesType of LanguageObject-Oriented, General PurposeObject-Oriented & Functional General PurposeGeneral PurposeSpecifically for Data Scientists.Needs conversion into Scala/Python before productizingConcurrencySupport ConcurrencySupport ConcurrencyDoes not Support ConcurrencyNAEase of UseVerboseLesser Verbose than ScalaLeast VerboseNAType SafetyStatically typedStatically typed (except for Spark 2.0 Data frames)Dynamically TypedDynamically TypedInterpreted Language (REPL)NoNoYesYesMaturated machine learning libraries availability/ SupportLimitedLimitedExcellentExcellentVisualization LibrariesLimitedLimitedExcellentExcellentWeb Notebooks SupportIjava Kernel in Jupyter NotebookApache Zeppelin Notebook SupportJupyter Notebook SupportR NotebookWhich language is better for Spark and Why?With the info we gathered for the languages, let's move to the main question i.e. which language to choose for Spark? My answer is not a straightforward single language for this question. I will state my point of view for choosing the proper language: If you are a beginner and want to choose a language from learning Spark perspective. If you are organization/ self employed or looking to answer a question for solutioning a project perspective. I. If you are beginner:If you are a beginner and have no prior education of programming language then Python is the language for you, as it’s easy to pick up. Simple to understand and very user-friendly. It would prove a good starting point for building Spark knowledge further. Also, If you are looking for getting into roles like ‘data engineering’, knowledge of Python along with supported libraries will go a long way. If you are a beginner but have education in programming languages, then you may find Java very familiar and easy to build upon prior knowledge. After all, it grapevine of all the languages.  If you are a hardcore bigdata programmer and love exploring complexities, Scala is the choice for you. It’s complex but experts say if once you love Scala, you will prefer it over other languages anytime.If you are a data scientist, statistician and looking to work with Spark, R is the language for you. R is more science oriented than Python. II. If you are organization/looking for choice of language for implementations:You need to answer the following important questions before choosing the language:Skills and Proficiency: Which skill-sets and proficiency over language, you already have with you/in your team?Design goals and availability of features/ Capability of language: Which libraries give you better support for the type of problem(s) you are trying to solve.Performance implications Details of these explained below: 1. Skillset: This is very straightforward. Whichever is available skill set within a team, go with that to solve your problem, after evaluating answers of other two questions. If you are self-employed, the one you have proficiency is the most likely suitable choice of language.  2. Library Support:  Following gives high-level capabilities of languages:R: Good for research, plotting, and data analysis.Python: Good for small- or medium-scale projects to build models and analyse data, especially for fast start-ups or small teams.Scala/Java: Good for robust programming with many developers and teams; it has fewer machine learning utilities than Python and R, but it makes up for it with increased code maintenance.In my opinion, Scala/Java can be used for larger robust projects to ease maintenance. Also, If one wants the app to scale quickly and needs it to be robust, Scala is the choice.Python and R: Python is more universal language than R, but R is more science oriented. Broadly, one can say Python can be implemented for Data engineering use cases and R for Data science-oriented use cases. On the other hand, if you discover these two languages have about the same library support you need, then pick the one whose syntax you prefer. You may find that you need both depending on the situation. 3. Performance: As seen earlier in the article, Scala/ Java is about 10x faster than Python/R as they are JVM supported languages. However, if you are writing Python/R applications wisely (like without using UDFs/ Not sending data back to the Driver etc) they can perform equally well.ConclusionFor learning, depending upon your prior knowledge, Python is the easiest of all to pick up. For implementations, Choice is in your hands which language to choose for implementations but let me tell you one secret or a tip, you don’t have to stick to one language until you finish your project. You can divide your problem in small buckets and utilize the best language to solve the problem. This way, you can achieve balance between optimum performance, availability, proficiency in a skill, and sub-problem at hand.  Do let us know how your experience was in learning the language comparisons and the language you think is better for Spark. Moreover, which one you think is “the one for you”, through comments below.
Rated 4.5/5 based on 1 customer reviews
8241
Scala Vs Python Vs R Vs Java - Which language is b...

One of the most important decisions for the Big da... Read More

What is Context in React? How to use Context in React?

What the hack is Context?Have you ever wondered about passing data or using states in between different components without using Props? Or passing a state from Parent to Child component without manually passing a prop at every level?  Let’s understand with an example below:Here we have a parent component app.js where we have defined our states. We want to access the data of state in the last child which is “Child 1.2” in the below chart.app.js Parent ComponentThe ideal or older approach in React is to pass the data from the root component to the last component is via Props. We have to pass props in each intermediary level so as to send in the last level. While this approach also works, the real problems begin if data is needed on a different branch i.e Child 2.1 or Child 2.2 in above chart…In order to solve this problem, we need to pass data from the root/top level of the application through all the intermediate components to the one where we want to pass the data, even though some intermediate components don't even need it.  This mind-numbing process is known as prop drilling,  Prop Drillingwhere you’re passing the state from your root component to the bottom and you end up passing the data via props through components that do not even necessarily need themOne really good solution to solve the above problem is using Context According to the React documentation:  “Context provides a way to pass data through the component tree without having to pass props down manually at every level”Ordinarily, we’d have used any state management library like Redux or have used HOC’s to pass the data in a tedious manner. But what if we don’t want to use it all? Here comes the role of new Context API!In layman words, it gives an approach to make specific data available to all components throughout the React component tree regardless of how deeply nested those components are.Context is just like a global object to the subtree of the React component.When to use the Context APIThe Context API is convenient for sharing data that is either global, such as setting the header and footer theme of a website or logic of user authentication and many more. In cases like these, we can use the Context API without using any extra library or external modules. It can also be used in a multilingual application where we want to implement multiple languages that can be translated into the required text with the help of ContextAPI. It will save prop-drilling   In fact, in any situation where we have to pass a prop through a component so it reaches another component, inside down the tree is where we can use the Context API.Introducing The Context APIThe context API is a way to pass data from top component to bottom ones, without manually passing it to via props. Context is fundamentally utilized when some data needs to be accessible by numerous components at different nesting levels. To create a new Context, we can use the React createContext function like below: const MyContext = React.createContext(defaultValue);In React, data is often passed from a parent to its child component as a property. Here, we can also omit the default value which we have passed to the context, if needed.React data passing from parent to its child Let’s Get Started With ContextThree things are needed to tap into the power of context: 1. The context itselfTo create a context we can use React.createContext method which creates a context object. This is used to ensure that the components at different level can use the same context to fetch the data.   In React.createContext, we can pass an input parameter as an argument which could be anything or it can be null as well.import React from `react';  const ThemeContext = React.createContext('dark');  // Create our context        export default ThemeContext;In this example, a string is passed for the current Context which is “dark”. So we can say, the current theme required for a specific component is Dark.   Also, we have exported the object so that we can use it in other places. In one app, React also allows you to create multiple contexts. We should always try to separate context for different purposes, so as to maintain the code structure and better readability. We will see that later in our reading.   What next?? Now, to utilize the power of Context in our example, we want to provide this type of theme to all the components.  Context exposes a pair of elements which is a Provider Component and a Consumer Component.2. A context providerOkay, so now we have our Context object. And to make the context available to all our components we have to use a Provider.   But, what is Provider? According to the React documentation:"every context object comes with a Provider React component that allows consuming components to subscribe to context changes"In other words, Provider accepts a prop (value) and the data in this prop can be used in all the other child components. This value could be anything from the component state.// myProvider.js import React from 'react'; import Theme from './theme'; const myProvider = () => ( ...   ); export default myProvider;We can say that a provider acts just like a delivery service.prop finding context and deliverling it to consumerWhen a consumer asks for something, it finds it in the context and delivers it to where it's needed.But wait, who or what is the consumer???3.  A context consumer What is Consumer? A consumer is a place to keep the stored information. It can request for the data using the provider and can even manipulate the global store if the provider allows it. In our previous example, let’s grab the theme value and use it in our Header component. // Header.js   import React from 'react'; import Theme from './theme';   const Header = () => (                        {theme => Selected theme is {theme}}             );   export default Header;Dynamic Context:   We can also change the value of the provider by simply providing a dynamic context. One way of achieving it is by placing the Provider inside the component itself and grabbing the value from component state as below:// Footer.js   import React from 'react';   class Footer extends React.Component {    state = {        theme: 'dark'    };      render() {        return (                                                );    } }Simple, no? We can easily change the value of  the Provider to any Consumer.Consuming Context With Class-based ComponentsWe all pretty know that there are two methods to write components in React, which is Class based components and Function based components. We have already seen a demo of how we can use the power of Context in class based components.  One is to use the context from Consumer like “ThemeContext.Consumer” and the other method is by assigning context object from current Context to contextType property of our class.import React, { Component } from "react"; import MyThemeContext from "../Context/MyThemeContext"; import GlobalTheme from "../theme";   class Main extends Component {    constructor() {        super();    }    static contextType = MyThemeContext;  //assign context to component    render() {        const currentTheme = GlobalTheme[this.context];        return (            ...        );    }   }There is always a difference in how we want to use the Context. We can either provide it outside the render() method or use the Context Consumer as a component itself.  Here in the above example, we have used a static property named as contextType which is used to access the context data. It can be utilized by using this.context. This method however, limits you consuming, only one context at a time.Consuming Context With Functional ComponentsContext with Functional based components is quite easy and less tedious. In this we can access the context value through props with the help of useContext method in React. This hook (useContext) can be passed in as the argument along with our Context to consume the data in the functional component.const value = useContext(MyContext);It accepts a context object and returns the current context value. To read more about hooks, read here.  Our previous example looks like:import React, { useContext } from 'react' import MyThemeContext from './theme-context'   const User = props => {    const context = useContext(MyThemeContext)    return ...Now, instead of wrapping our content in a Consumer component we have access to the theme context state through the ‘context’ variable.But we should avoid using context for keeping the states locally. Instead of  conext, we can use local state there.Use of Multiple ContextsIt may be possible that we want to add multiple contexts in our application. Like holding a theme for the entire app, changing the language based on the location, performing some A/B testing, using global parameters for login or user Profile… For instance, let’s say there is a requirement to keep both Theme context and userInfo Context, the code will look like as:       ...   It’s quite possible in React to hold multiple Contexts, but this definitely hampers rendering, serving ‘n’ number of contexts in ‘m’ component and holding the updated value in each rendered component.To avoid this and to make re-rendering faster, it is suggested to make each context consumer in the tree as a separate node or into different contexts.                 And we can perform the nesting in context as:    {theme => (                    {colour => (                Theme: {theme} and colour: {colour}            )}            )} It’s worth noting that when a value of a context changes in the parent component, the child components or the components’ holding that value should be rerendered or changed. Hence, whenever there is a change in the value of provider, it will cause its consumers to re-render.ConclusionDon’t you think this concept is just amazing?? Writing a global context like theme or language or userProfile and using the data of them directly in the child or other components? Implementing these stateful logic by global preferences was never so easy, but Context made this transportation job a lot simple and achievable! Hope you find this article useful. Happy Coding!Having challenge learning to code? Let our experts help you with customized courses!
Rated 4.0/5 based on 1 customer reviews
7928
What is Context in React? How to use Context in Re...

What the hack is Context?Have you ever wondered ab... Read More

How to use sys.argv in Python

The sys module is one of the common and frequently used modules in Python. In this article, we will walk you through how to use the sys module. We will learn about what argv[0] and sys.argv[1] are and how they work. We will then go into how to parse Command Line options and arguments, the various ways to use argv and how to pass command line arguments in Python 3.x In simple terms,Command Line arguments are a way of managing the script or program externally by providing the script name and the input parameters from command line options while executing the script. Command line arguments are not specific just to Python. These can be found in other programming languages like C, C# , C++, PHP, Java, Perl, Ruby and Shell scripting. Understanding sys.argv with examples  sys.argv is a list in Python that contains all the command-line arguments passed to the script. It is essential in Python while working with Command Line arguments. Let us take a closer look with a few examples. With the len(sys.argv) function, you can count the number of arguments. import sys print ("Number of arguments:", len(sys.argv), "arguments") print ("Argument List:", str(sys.argv)) $ python test.py arg1 arg2 arg3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']Module name to be used while using sys.argv To use sys.argv, you will first need to the sys module. What is argv[0]? Remember that sys.argv[0] is the name of the script. Here – Script name is sysargv.py import sys print ("This is the name of the script: ", sys.argv[0]) print ("Number of arguments: ", len(sys.argv)) print ("The arguments are: " , str(sys.argv))Output:This is the name of the script:  sysargv.py                                                                               Number of arguments:  1                                                                                                 The arguments are:  ['sysargv.py']What is "sys. argv [1]"? How does it work? When a python script is executed with arguments, it is captured by Python and stored in a list called sys.argv. So, if the below script is executed: python sample.py Hello Python Then inside sample.py, arguments are stored as: sys.argv[0] == ‘sample.py’ sys.argv[1] == ‘Hello’ sys.argv[2] == ‘Python’Here,sys.argv[0] is always the filename/script executed and sys.argv[1] is the first command line argument passed to the script . Parsing Command Line options and arguments  Python provides a module named as getopt which helps to parse command line options and arguments. Itprovides a function – getopt, whichis used for parsing the argument sequence:sys.argv. Below is the syntax: getopt.getopt(argv, shortopts, longopts=[]) argv: argument list to be passed.shortopts: String of short options as list . Options in the arguments should be followed by a colon (:).longopts: String of long options as list. Options in the arguments should be followed by an equal sign (=). import getopt import sys   first ="" last ="" argv = sys.argv[1:] try:     options, args = getopt.getopt(argv, "f:l:",                                ["first =",                                 "last ="]) except:     print("Error Message ")   for name, value in options:     if name in ['-f', '--first']:         first = value     elif name in ['-l', '--last']:         last = value   print(first + " " + last)Output:(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python getopt_ex.py -f Knowledge -l Hut Knowledge Hut (venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python getopt_ex.py --first Knowledge –last Hut Knowledge HutWhat are command line arguments? Why do we use them? Command line arguments are parameters passed to a program/script at runtime. They provide additional information to the program so that it can execute. It allows us to provide different inputs at the runtime without changing the code. Here is a script named as argparse_ex.py: import argparse parser = argparse.ArgumentParser() parser.add_argument("-n", "--name", required=True) args = parser.parse_args() print(f'Hi {args.name} , Welcome ')Here we need to import argparse package Then we need to instantiate the ArgumentParser object as parser. Then in the next line , we add the only argument, --name . We must specify either shorthand (-n) or longhand versions (--name)  where either flag could be used in the command line as shown above . This is a required argument as mentioned by required=True Output:  (venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py --name Nandan  Hi Nandan , Welcome  (venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py -n Nandan  Hi Nandan , Welcome The example above must have the --name or –n option, or else it will fail.(venv) C:\Users\Nandank\PycharmProjects\DSA\venv>python argparse_ex.py --name   usage: argparse_ex.py [-h] --name NAME argparse_ex.py: error: the following arguments are required: --namePassing command line arguments in Python 3.x argv represents an array having the command line arguments of thescript . Remember that here, counting starts fromzero [0], not one (1). To use it, we first need to import sys module (import sys). The first argument, sys.argv[0], is always the name of the script and sys.argv[1] is the first argument passed to the script. Here, we need to slice the list to access all the actual command line arguments. import sys if __name__ == '__main__':     for idx, arg in enumerate(sys.argv):        print("Argument #{} is {}".format(idx, arg))     print ("No. of arguments passed is ", len(sys.argv))Output:(venv) C:\Users\Nandank\PycharmProjects\DSA\venv\Scripts>python argv_count.py Knowledge Hut 21 Argument #0 is argv_count.py Argument #1 is Knowledge Argument #2 is Hut Argument #3 is 21 No. of arguments passed is  4Below script - password_gen.py is used to generate a secret password by taking password length as command line argument.import secrets , sys, os , string ''' This script generates a secret password using possible key combinations''' ''' Length of the password is passed as Command line argument as sys.argv[1]''' char = string.ascii_letters+string.punctuation+string.digits length_pwd = int(sys.argv[1])   result = "" for i in range(length_pwd):     next= secrets.SystemRandom().randrange(len(char))     result = result + char[next] print("Secret Password ==" ,result,"\n")Output:(venv) C:\Users\Nandank\PycharmProjects\DSA\venv\Scripts>python password_gen.py 12 Secret Password == E!MV|,M][i*[Key takeaways Let us summarize what we've learnt so far. We have seen how to use the sys module in Python, we have looked at what areargv[0] and sys.argv[1] are and how they work, what Command Line arguments are and why we use them and how to parse Command Line options and arguments. We also dived into multiple ways to use argv and how to pass command line arguments in Python 3.xHope this mini tutorial has been helpful in explaining the usage of sys.argv and how it works in Python. Be sure to check out the rest of the tutorials on KnowledgeHut’s website and don't forget to practice with your code! 
Rated 4.0/5 based on 14 customer reviews
5987
How to use sys.argv in Python

The sys module is one of the common and frequently... Read More

20% Discount