Jan 6, 2020

[2020 UPDATED] PYTHON LIST APPEND || PYTHON LIST APPEND LIST

Python List Append, Extend, Insert, Remove


This post is all about the String and python list append, extend, clear, remove, insert, pop, reverse, count and copy But we focused here on PYTHON LIST APPEND this is our topic.


In python, we can use a different kind of string in a single command, not like C and C++ languages where we have to use an array and we have to mention the data type of the array, here we have to just give the name to the array and we can use different type of data type in the single list like in single list name[] I can use the data types likes float, int, char, etc.

The syntax for assigning the values 

num = [11,12,34,15,54,23,45,22,63,47,13]
here we have used the variable name ''num''' and by using equal to sign we are assigning the values to the variable "num" but you need to remember that for assigning multiple values to num you have to use Square bracket and by using ","(comma) we can assign multiple values in python. this is the syntax which we should remember if we want to use python list append, extend, insert, remove in programing.

If you look at the above example where we use num if we have to call the specific number from that list or if we have to exclude some number from that list then we can use the one simple syntax.

How to call the values from the list

name_of_variable[index_number]
let's take same example
num = [11,12,34,15,54,23,45,22,63,47,13]

Indexing in the python language is start with "0"
so from the above list

11-index number -0
12-index number -1
34-index number -2
54-index number -3
23-index number -4
45-index number -5
22-index number -6
63-index number -7
47-index number -8
13-index number -9

let's have some codes from IDLE python 3


>>> num = [11,2,35,14,54,36,48,12,36]

>>> num[0]               here we called the value by index number
11                               we got the 11 because 11 is on index no. 0
>>> num[1]
2
>>> num[6]
48

>>> num[9]
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    num[9]
IndexError: list index out of range

   here we get en error because our range is from 0-8 and we are asking the value which is not present in our list so that it gives error so remember that when you are calling any value it should be in the range of given list.

If You Want To Print in Between values from List

If you have the list num = [11,12,34,15,54,23,45,22,63,47,13] and you want to print the number from 34-63 what should you do or if you want to print the number from 15 till the end then you have use the syntax which is given below,
  • I want to print the numbers from 34-63
  • 34 index number is 2
  • 63 index number is 8
  • Syntax :  num[2:9]
  • Output: [34,15,54,23,45,22,63]
  • why we give index number 9 as we want the numbers till 8
  • that means it excludes the value at 9 and print the value just before it.
Let's perform some code then you can understand that Blue Colour output and Black Colour is input in the below program.


>>> number= [11,36,14,86,45,12,365,44,56,87]

>>> number[3:9]
[86, 45, 12, 365, 44, 56] number shows from 3-8

>>> number[2:6]           
[14, 86, 45, 12]    Numbers shows from 2-5

>>> number[7:8] Number shows from 7-7 that means one number
[44]

>>> number[5:]
[12, 365, 44, 56, 87] what is different here we did not assign any value after ':' that means it shows all the values after 5 as we did not mention the limit here.

>>> number[:3]
[11, 36, 14]
Here we didn't mention the index number so that it shows the number from starting to the later limit which is 3.

>>> number[2:]
[14, 86, 45, 12, 365, 44, 56, 87] after 2 we are getting all results.

Using String in List

As I mentioned earlier you can use Strings in the python list lets directly see the syntax for that

Syntax: Variable_name = [list values]

example:- 
>>>universe =['moon','star','sun','mercury','digitalzak']
>>> universe[3]
'mercury'
>>> universe[1]
'star'

OPERATIONS USING LIST

  • append
  • insert
  • clear
  • remove
  • pop
  • reverse
  • count
  • copy
Syntax

let's make the one list called "num1"

num1 = [1,3,5,7,9,6,3,4,5]
"We are going to use all of the above operations on list "num1" for better understanding"
"For using this functions we have to use the list_name.operation_name(parameters)" 

Python list APPEND

This function is used to add the new value in the given list at the end of the list


>>> num1 = [1,3,5,7,9,6,3,4,5]
>>> num1.append(22)
>>> num1
[1, 3, 5, 7, 9, 6, 3, 4, 5, 22]  22 is added at the last of the list

Python list INSERT

By using this function you can add any value at a specific index number the syntax is num1.insert(index_no , Value)

>>> num1
[1, 3, 5, 7, 9, 6, 3, 4, 5, 22]  this is  over given list

>>> num1.insert(3,71)  By using the .insert fucntion we have inserted the 71 at the index number 3.

>>> num1
[1, 3, 5, 71, 7, 9, 6, 3, 4, 5, 22] when we press enter we get the result as you can see from the above output that the 71 is present at the 3rd index number.

Python list Remove

this function is used to delete any number or string from the list in python it is very important for using the list in python because sometimes we have to make a program in which some entities have to left the list then we can use this function for that.

We can do that by giving that entity directly or by giving the index number for that entity.


>>> num1

[1, 3, 5, 71, 7, 9, 6, 3, 4, 5, 22]  This is our updated list where we added the number 71 now I want to remove that number we can use the remove function here.

>>> num1.remove(71) Here I am directly giving the name or value of the entity.

>>> num1
[1, 3, 5, 7, 9, 6, 3, 4, 5, 22]  Number 71 is removed form the list

"you cannot remove value by index number in remove function for that we need .pop function"

Now let's try to remove 9 by its index number because you can see 3 comes two times in list so that it's important to give the index number to show the compiler .the number you are interested to remove.


Python list pop

this function is used to remove the value from the list by its index number lets see it by the example.


>>> num1

[1, 3, 5, 7, 9, 6, 3, 5, 22]
>>> num1.pop(4)
9   Here it shows which number is excluded from the list
>>> num1
[1, 3, 5, 7, 6, 3, 5, 22]

What will happen if we use .pop without index value

if we use .pop function without index value it automatically removed the last element from the list.
>>> num1
[1, 3, 5, 7, 6, 3, 5, 22]
>>> num1.pop() here I didn't give any index number so it remove last element from the list which is 22 as you can see in the output it shows below.
22
>>> num1
[1, 3, 5, 7, 6, 3, 5]  the last element is removed from the list.


As you can see that the number 9 is removed by .pop function by using the index number.

This is all for the today's post hope you enjoy learning the python list append, insert, remove and all python is one of the most trending languages nowadays and it is also the one of the easiest languages which can be learned by anyone if you want to learn more about the python then you can follow my blog and also you can give comment below if you have any difficulty regarding python I will try to give my 100 percent to answer your question thank you soo much.






No comments:

Post a Comment

Popular Blog Posts