Lists



👈Previous Topic


Hello folks !!!
Today we are going to see different data structures in python which are very important concepts. I strongly recommend you to workout the examples yourselves so that you can get a clear idea. 

Open your Jupyter Notebook before you start...........

The data structures available in python are :
  1. Lists
  2. Dictionary
  3. Tuple
  4. Sets
Today we will discuss about lists.

Lists

  • The lists are nothing complex. They are just like an array in C or C++.
  • The main difference between an array and lists is that arrays are homogeneous, ie., they can hold only values of same data type, but the lists can hold different datatype values and thus they can be either homogeneous or heterogeneous. 
  • The declaration of a list in python is very simple. We use square brackets -'[ ]' to denote lists. You can declare a list as: li=[].Here li is the variable name, [ ] is the declaration syntax. This line denotes that 'li' is a variable which holds a list.
  • The declaration and initialization can be combined into single step as li=[1,2,3,4]. This is a homogeneous list. Let us see examples different lists.

Do you still feel lists are confusing !?
Now I will give you a day-to-day example. Imagine you returned from the market. You have to keep the vegetables you bought in the fridge. what would you do ? You will put all tomatoes together in a bag, all brinjals together in a bag and so on. Thus each type of vegetables in a separate bag. Now each bag is (an array in C or C++) homogeneous list. Now imagine that you are purchasing some items in a supermarket. You will put whatever things you take into the single basket or a trolley, it may be an eatable, a toy, pencil, eraser, grocery and so on. Now all types of items are in one container. This is a heterogeneous list. 

Lists and their features:

  • The items in lists are changeable, ie., You can change the value using its index number. The index of a list is:

The ones in the top is reverse index and the ones at the bottom is the normal index. To access an element in the list you just have to write list_name[index]. In the above list, let us consider its name as 'li'. To access the element '22', you need to write li[3] in normal indexing method and li[-2] in reverse indexing. 
  • Several built-in methods are available to do operations with lists which can be seen in upcoming days.
  • Lists allow a value to repeat any number of times.
  • Negative indexing is allowed in lists which allows it access from the reverse.
  • Lists can be spliced using the index number. Splicing allows to show only the items in the given index range. For example:

Here li[0:4] is normal splicing. The syntax is list_name[start:end].Note that it displays the value from the start index to (end-1). The start and end can be any value but must not exceed the whole list's length. To display the last 'n' elements in a list, use reverse indexing. Note that in reverse indexing the index must also be mentioned in reverse and a minus symbol should be prefixed. You have to write li[-4:-1]and not as li[-1:-4]. Note that you can even leave either the end or start empty. If you leave the end empty as in li[-3:]the elements from the reverse index -3 to the last element in the list will be displayed. 
  • If you assign a list a variable, that variable also becomes a list data type. 
  • Two lists can be concatenated also. For example:

Here when you assign 'li' to 'z', 'z' also becomes a list and has the values same as 'li'. The '+' symbol concatenates two lists. It append the second list at the end of the first one.



Exercise:
1. create a list of characters. The characters to be in the list are a,b,c,d,e. (hint : characters must be enclosed within single quotes)
2. consider the list : [1,2,3,7,4,8,5]
  • Display the third last element using reverse indexing.
  • Display the first 3 elements only.
  • Display the last 4 elements using reverse index splicing technique.
3. Create two separate lists with elements 1,4,2,6,8 in one list and the elements j,k,l,b in another list. Concatenate them and assign to a new list named 'mixed_list'. Now from the new list:
  • Display the character 'l'
  • Display all elements from the index 5.
  • Splice the list and assign to two separate lists namely 'numbers' and 'alphabets', such that all integers are assigned to the list 'numbers' and all the alphabets are assigned to the list 'alphabets'. Your output should look like : 
                     numbers : 1,4,2,6,8   
                     alphabets: j,k,l,b


Let us see the built-in methods associated with python lists tomorrow.

Try out all these exercises and let me know if you face any trouble. If you get any error share the ipynb file of your jupyter notebooks on which you tried these exercises. Today I will post how to download your notebook's ipynb file. Mail it to : crazygirlaks@gmail.com


Next Topic👉

Comments