Python programming language has four collection data types namely List, Tuple, Set, and Dictionary. A list is a mutable and ordered collection i.e., elements of the list can be changed and it maintains the order of insertion of its items. Because of the property of order maintaining, each element of the list has a fixed index and it permits the list to have duplicate elements. In Python, list is very useful as it has the ability to contain non-homogeneous elements.
Following are some operations which can be performed on the list:
List of numbers: [10, 20, 30] List of Strings: ['Geeks', 'For', 'Geeks'] List of non-homogeneous elements: [10, 'Geeks', 20.89, 'for', 30, 'geeks'] Size of the Non-homogeneous list: 6 Original List: ['Geeks', 'for', 'Geeks'] After adding an element thelist becomes: ['Geeks', 'for', 'Geeks', 'the'] After adding an element atindex 3 the list becomes: ['Geeks', 'for', 'Geeks', 'is', 'the'] After adding 3 elements at theend, the list becomes: ['Geeks', 'for', 'Geeks', 'is', 'the', 'best', 'CS', 'website'] After removing an elementthe list becomes: ['Geeks', 'for', 'Geeks', 'is', 'best', 'CS', 'website'] After removing an element from index 3 the list becomes: ['Geeks', 'for', 'Geeks', 'best', 'CS', 'website']
To get more in-depth knowledge about python list click here.
Python arrays are also a collection but its items are stored at contiguous memory locations. It can store only homogeneous elements(elements of the same data type). Arrays are very beneficial in performing mathematical operations on the elements. Unlike lists, arrays can not be declared directly. To create an array the
array module must be imported and the syntax of the declaration is different from that of the list.
Following are some operations which can be performed on the array:
Array a1: array('i', [10, 20, 30]) Elements of the arraya1 is : 10, 20, 30, Elements of the arraya2 is : 1.5, 2.4, 3.9, Original elements of thearray a1 is : 10 20 30 Elements of the array a1after adding an elementat last: 10 20 30 40 Elements of the array a1after adding an elementat index 3: 10 20 30 35 40 Array a1 after removingelement 20: 10 30 35 40 Array a1 after removingelement of index 2: 10 30 40
To get more in-depth knowledge about python array click here.
Similarities in Python list and array
Both array and lists are used for storing the data: The purpose of both the collection is to store the data. While the list is used to store homogeneous as well as non-homogeneous data, an array can store only homogeneous data.
List of Strings: ['Geeks', 'For', 'Geeks'] List of non-homogeneous elements: [10, 'Geeks', 20.89, 'for', 30, 'geeks'] Elements of the array is : 1.5, 2.4, 3.9,
List and Array both are mutable: List, as well as the array, have the ability to modify their elements i.e., they are mutable.
Original list: ['Geeks', 1, 'Geeks'] Modified list: ['Geeks', 'for', 'Geeks'] Original array: 10 20 30 37 50 Modified array: 10 20 30 40 50
Elements of both list and array can be accessed by index and iteration: In order to access the elements of list and array, we have the option of using index number or we can traverse them by iteration.
List1 elements: [10, 20, 30, 20, 10, 40] Element at index 0: 10 Element at index 1: 20 Element at index 3: 20 Element at index 4: 10 Element at last index: 40 Element at 3rd last index: 20 Accessing through iteration: 10 20 30 20 10 40 Array1: array('i', [10, 20, 30, 40, 50, 60]) Element of Array1 at index 3: 40 Accessing through iteration: 10 20 30 40 50 60
List and array both can be sliced: Slicing operation is valid for both the list and array to get a range of elements.
List elements from index 1 to 4: [20, 30, 20] List elements from index 2 to last: [30, 20, 10, 40] Slicing elements of the array in a range from index 1 to 4: array('i', [20, 30, 40]) Slicing elements of the array from 2nd index till the end: array('i', [30, 40, 50, 60])
Differences between the Python list and array:
Difference in creation: Unlike list which is a part of Python syntax, an array can only be created by importing the array module. A list can be created by simply putting a sequence of elements around a square bracket. All the above codes are the proofs of this difference.
Memory consumption between array and lists: List and array take a different amount of memory even if they store the same amount of elements. Arrays are found to be more efficient in this case as they store data in a very compact manner.
Size of each element of list in bytes: 48 Size of the whole list in bytes: 48000 Size of each element of the array in bytes: 4 Size of the whole array in bytes: 4000
Performing mathematical operations: Mathematical operations like dividing or adding each element of the collection with a certain number can be carried out in arrays but lists do not support these kinds of arithmetic operations. Arrays are optimized for this purpose while to carry out these operations in the list, operations have to be applied to every element separately.
Lists don't support list + int Modified array: [6 7 8]
Resizing: Arrays once declared can not be resized. The only way is to copy the elements of the older array into a larger sized array. While the list can be re-sized very efficiently.
Data that can be stored: List can store both homogeneous as well as non-homogeneous data while arrays support the storage of only homogeneous data.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Python | Program to count number of lists in a list of lists
- Python Object Comparison : "is" vs "=="
- Python | Excel File Comparison
- Comparison of Python with Other Programming Languages
- Chaining comparison operators in Python
- Python | Consecutive String Comparison
- Python | Zipping two lists of lists
- Python | Data Comparison and Selection in Pandas
- Python | Find Hotel Prices using Hotel price comparison API
- Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
- Python Lists
- Python | Difference between two lists
- Python | Dividing two lists
- Python - K Summation from Two lists
- Python - Key Lists Summations
- Python | Using 2D arrays/lists the right way
- Python | Iterating two lists at once
- Python | Union of Value Lists
- Python | Average of two lists
- Python | All possible permutations of N lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet