Blog

Filter posts by Category Or Tag of the Blog section!

List Comprehensions in python

Saturday, 17 June 2017

I'm a Dot Net developer, but sometimes I jump on other programming languages and I think it helps me to comprehend my main field of activity easily. I was just working with the list in Python. Let's create a simple list:

 

mylist = ["angular", "javascript", "typeScript"]

 

print(mylist)  ##For printing the list

  

for x in mylist  ##For creating a loop through the list

  print(x)

 

print(len(mylist )) ##For getting the length of the list

 

mylist.append("backbon")  ##For adding an Item

 

mylist.Remove("javascript")  ##For removing an item

 

mylist.clear() ##For clearing the list

 

Del mylist  ##For deleting the whole list

 

mylist[1]   ##For getting the member of an index

 

mylist.Insert(4,"meteor")   ##For inserting an item to a specific index

 

mylist.Pop()   ##To removing the last member of the list

 

mylist.reverse()  ## for reversing the list

 

Python also supports negative indexes. For example, the index of -1 refers to the last item, -2 refers to the second last item etc.

 

print(Mylist[-2]) ## output:javascript

 

And it worth mentioning that, python supports for slice as well

 

print(mylist[1:3])   ## elements 1rd to 3th

 

print(mylist[:-3])  ## elements beginning to 2th

 

print(my_list[2:]) ## elements 3th to end
 

Category: Software

Tags: Python

comments powered by Disqus