Hello Folks....!!!
Today we will look into the methods associated with the Lists and some simple programs.
There are several built-in methods associated with the Lists in python.
1. append():
- This method is used to add new elements at the end of the lists.
- You can use this method on empty lists also. It is not mandatory that the list must contain some elements in it.
- You can append items to lists that already contains elements also. For example : Here the 'list1' is initially empty. We can add items to it using append method.
- Note that a new item can be added at any point in the program.
2. list():
- This method is used to convert another data type/structure into list.
- We mostly convert Strings to lists for some processing.
- If you pass a word to this method, it splits the word into characters and converts it into a list.
- This method serves as the first step for various operations which we will see in upcoming part.
3. join():
- This method is used to convert a list into string.
- The syntax is symbol.join( List_name).
- Here symbol refers to the symbol that must appear after each character or word in the list. It might be either a space, a comma, a period(.), a semicolon or anything.
- .join() is the method name. we have to pass the list name as argument to this method. For Example:
Note : The join method can accept
only strings. Our list contains integer items and thus we get error. So
we need to pass a List containing String or characters to join()
method.
Now it works fine!!! The numbers in list are joined with comma after
each item. Similarly list of words can also be joined. Just leave a
space instead of comma (like this-' '.join()) if you need space between
each list element.
But now you might think, Every time how can I expect the list to be a
character or string. Is there any method using which i can dynamically
convert my list to String or any other type...???
The answer is yes, there exists a method. That method is not
specifically for lists only. It can be used with normal variables and
other data structures in python also. The method is map().
- map()takes two arguments. The first is 'the type to which you need to convert' and the second is 'the data structure which you need to convert'. For example :
- Here what we have is 'li1', a list of odd integers. Now to convert it into string we use the method map(). The first argument is 'str'- we need to convert the items into string. The second one is 'li1'- the list which we need to convert.
- Note: We cannot use the output of map() method directly. So we need to convert it into appropriate data structure. Here we have converted it into list. Thus we have written list(map(str,li1)).
So reading these kind of inputs will be very easy if you use python
3. The method which I have shown below to get inputs will be very very
useful in some cases.
So when you need to get three inputs and the test-case has all the
three inputs in a single line you can use this.
Explanation for the above code:
- input().split()- as you all know, it is for getting user input and split them on space and store in corresponding variables.But the problem is that it reads the input as string only.
- map()- This helps us to convert the string values read into desired type and maps it into respective variables.
4. remove():
- This method is used for removing an element from the list.
- The syntax is List_name.remove(item).
- If an element appears more than once in a list, only its first occurrence is removed and not all. To remove all instances we need to use filter() and lambda which will be discussed later.
In the second example, you can find only the first occurrence of 5 is
removed.
5. range():
- This method is combined with list() method to create a list of contiguous elements in the given range.
- It accepts either one or two parameters (start,end). If you specify only one argument, then it is considered as 'end' parameter.
- This method creates the list of items in the range start-(end-1). So if you want the numbers from 1 to 10, you should mention range(1,11).
- But this method can be used only for numbers and not for strings or alphabets.
Another interesting feature with the lists is, if you want to
create a list only by repeating a value then you can write as:
Here li should contain five 1's. Thus written as [1] *5 which means
li is a list where '1' should be repeated five times.
Exercises:
1. Read the inputs given in single line and convert them into float
and store in a single list. The input is 4.5 5.9 6.2 7.3
2. Join the list with appropriate symbol to form a sentence:
- li1=['I','am','coding','now']
- li2=[22,33,55,77] --- Output : 22,33,55,77 are multiples of 11.
3. Consider the list : [1,2,'a','4','6',5,7]. Remove 4 from the
list and display. now remove a from the list. (** For characters quotes must be used. Anything can be a
character!!!!)
Thats all for today... See you with next content tomorrow..... Please
do practice the exercises that I give daily.... Ping me if you have any
doubts or getting any error.......
Next Page👉
Comments
Post a Comment