In python, the most basic data structure is sequence where a number is assigned by its position or index to each part of a sequence.The first index is zero, the second index is, and so on. Python has six built-in types of sequences, but in this tutorial, we walked with concept called as list which is defined as changeable or mutable data-structure in Python.
Lists are defined as the sequence of arbitary objects enclosed by square brackets. Lists are ordered and changeable, separated by commas and often consisting of number of compound data types. lists are a mutable type, i.e. it is possible to change their content.
>>> a = [1, 2.2, 'python'] >>> print (a) >>> print (a) output: [1, 2.2, 'python'] 2.2
Imagine, you have different values and say you have numbers, and likewise you have strings. If you want to group them together, Lists are used and this is similar if you have worked on arrays in any other programming langauges (C, C++, Java).
A Python list is an example of a heterogeneous ordered set, which is:
- Dynamicallly sized: You don’t need to specify a size when you first create your list, and it can expand to fit as many data as you need depending upon the limits set by the O/S.
- Heterogeneous : List in python can contain any other data element and may contain any mixture of data types in any given list. Python lists are so versatile that they are also containable.
- Ordered: They maintain their contents in the order dictated by the application and we are able to access their elements in one order after another
>>> nums = [5,6,8,-10,36] >>> print('The first value of list is -->', nums) >>> print('The last value of list is -->', nums) output: The first value of list is --> 5 The last value of list is --> 36
Slicing operator  can be used with list. List stores sequences indexed by integers, starting at zero. To extract a single item, use the indexing operator s[i] like this: Similar to string, square brackets can be used to access elements of the list, we can access the range of items using slicing. To obtain certain part of items we specify starting and ending index separated by colon ‘:’.
>>> a = [ 's', 'a', 1,2,3,'w'] >>> print (a[2:4]) output: [1,2]
Here, we have assigned the starting index 2 i.e ‘1’ and ending index 4 i.e 3. But item of index 4 is ignored and only 2nd and 3rd indexed items are shown. This extracts all items from s[i:j] whose index is in the range i<=k<j. If either index is omitted, the beginning or end of the list is assumed, respectively: Similarly, we can use negative slicing as follows:
>>> c = a[-4:-2] >>> print (c) output : [1,2]
In negative slicing, starting and ending index are reverse and negative sign(-) is introduced before them.
Can we have list of different data types ?
There are no inherent problems in having multiple data types in a list comparing to the array of the other programming languages that only stored one type of data.
Since lists can also store other compound data structures and other list along with other object references, processing or iterating through the list may become a bit more complex due to possible multiple layers, than just going through an array in a simple single level iteration. This is called Shallow and deep copying.
>>> names = ['Diwas', 'Sush', 'Sunil', 'Nana']
Imagine , if we made a mistake for the last item of names i.e. Nana should be spelled as Bae.
What will you do for this?
>>> print('Original list -->', names) >>> names[-1] = 'Bae' >>> print('New list for names --->', names) output: Original list --> ['Diwas', 'Sush', 'Sunil', 'Nana'] New list for names ---> ['Diwas', 'Sush', 'Sunil', 'Bae']
|append()||adds element to the end of the list and the length of the list itself will increase by one||nums.append(-6)|
|append adds elements at the end whereas insert adds at given index . syntax: listname.insert(index,item)||nums.insert(3, 5.5)|
|extend()||extends a list by appending elements from an iterable||nums.extend(names)|
|remove()||removes the element which you want to||nums.remove(3)|
|clear()||removes all the element from the list & returns empty||nums.clear()|
|sort()||sort the element of the given list in a specific order that maybe ascending or descending||nums.sort()|
nums.sort( reverse=True) # descending
2D list refers to rectangular data table which is generally defined as matrices or two dimensional array.
>>> matrix = [ [1,2,3], [4,4,5], [6,7,8] ] >>> print('Accessing first row:',matrix) >>> print('Accessing second element of first row:',matrix) output: Accessing first row: [1, 2, 3] Accessing second element of first row: 2
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to email@example.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.