100 Days of DevOps — Day 88-Lists in Python

Lists in Python

  • The list is a value that contains multiple values(mutable sequence)
>>> x = [1,2,’abc’,2.5]>>> x[1, 2, 'abc', 2.5]
  • We can access the item in the list with an integer index that start with 0(not 1)
>>> x[0]1
  • We can use the negative index to refer to the last item in the list
>>> x[-1]2.5
  • To get multiple items from the list use slice
>>> x[0:2][1, 2]#Basically grab every value of the list>>> x[0:][1, 2, 'abc', 2.5]
  • All the function that works with strings works in the same way with list eg:len()
>>> len(x)4
  • To delete the value from the list use del
>>> del x[0]>>> x[2, ‘abc’, 2.5]
  • We can convert a value into a list by passing it to the list() function
>>> list(‘hello’)[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
  • To find out the value in list use in operator
>>> ‘h’ in ‘hello’True#Opposite of that is not in operator>>> 'h' not in 'hello'False
  • Use of for loop with a list, so as you can see for loop iterate over the values in a list
>>> for i in x:… print(i)2abc2.5
  • range() function returns a list-like value which we can pass to the list() function if we need an actual value
>>> list(range(0,4))[0, 1, 2, 3]
>>> items = [“a”,”b”,”c”,”d”]
>>> for i in range(len(items)):
... print("Value at "+ str(i) + " is equal to: " + items[i])...Value at 0 is equal to: aValue at 1 is equal to: bValue at 2 is equal to: cValue at 3 is equal to: d
>>> a,b,c = 1,2,3>>> a1>>> b2>>> c3
  • index() list method returns the index of an item in the list
>>> test = [“a”,”b”,”c”,”d”]>>> test.index(“a”)0# Index method will raise the exception if it doesn't find the value>>> test.index("e")Traceback (most recent call last):File "<stdin>", line 1, in <module>ValueError: 'e' is not in list# Now in case of duplicate list it only return the index of first value>>> test = ["a","b","c","d","a","b"]>>> test.index("b")1
  • append() list method adds value to the end of a list
>>> test.append(“z”)>>> test[‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘b’, ‘z’]
  • insert() list method adds value anywhere inside a list
>>> test.insert(0,”hola”)>>> test[‘hola’, ‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘b’, ‘z’]
  • remove() list method removes an item, specified by the value from a list
>>> test.remove(“a”)>>> test  #Only first incident of that value is removed[‘hola’, ‘b’, ‘c’, ‘d’, ‘a’, ‘b’, ‘z’]
  • reverse(): A list can be reversed by calling its reverse() method
>>> a = [2,5,1,3,6,4]>>> a.reverse()>>> a[4, 6, 3, 1, 5, 2]
  • sort() list method sorts the items in a list
>>> test1 = [8,3,9,4,6,3]>>> test1.sort()>>> test1[3, 3, 4, 6, 8, 9]
>>> test2 = [1,2,3,”a”,”b”]>>> test2 = [1,3,2,6,5,”a”,”b”]>>> test2.sort()Traceback (most recent call last):File “<stdin>”, line 1, in <module>TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
>>> test3 = [‘a’,’b’,’A’,’B’]>>> test3.sort()>>> test3[‘A’, ‘B’, ‘a’, ‘b’]
>>> test3.sort(key=str.lower)>>> test3[‘A’, ‘a’, ‘B’, ‘b’]# The important use case of key, where we use len as a key(here sorting happens based on the length)
>>> b
['hello', 'how', 'are', 'u', 'Mr', 'Prashant']>>> b.sort(key=len)>>> b['u', 'Mr', 'how', 'are', 'hello', 'Prashant']
  • Sorted(): built-in function sorts any iterable series and returns a list
>>> x = [5,2,3,1]>>> sorted(x)[1, 2, 3, 5]>>> x[5, 2, 3, 1]
  • count(): Count returns the number of matching elements
>>> a = [1,2,3,4,1,23,5,6]>>> a.count(1)2
>>> test = [1,2,3,4]>>> test1 = test>>> test1[1, 2, 3, 4]>>> test1[0] = 9>>> test1[9, 2, 3, 4]>>> test[9, 2, 3, 4]
test1 = test
>>> a = “abc”>>> b = a>>> b = “efg”# Change in b will not impact a
>>> b
‘efg’>>> a‘abc
  • id(): returns a unique identifier for an object
>>> x = 100>>> id(x)4340331440>>> x = 50>>> id(x)4340329840
>>> x = [1,2,3]>>> import copy>>> y = copy.deepcopy(x)>>> y[1, 2, 3]>>> y[0] = 4>>> y[4, 2, 3]>>> x[1, 2, 3]
>>> a = [4,5,6]>>> b = a[:]>>> b[4, 5, 6]>>> id(a)4325208200>>> id(b)4325208328>>> b[0] = 10>>> b[10, 5, 6]>>> a[4, 5, 6]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Lakhera

Prashant Lakhera

4.91K Followers

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP