Learn C – Python data structure

Posted by

This  is in continuation of  Learn C-Python programming .

Key Takeaway : Understanding of data structure elements(Strings,Lists,Tuples) in Python


    • In C , single quote(‘A’) is used for single character and double(“ABC”) for string.
    • In Python, Both single and double quotes can be used to manipulate strings. ‘ABC’ and “ABC” are both valid strings. Backslash(\) can be used to escape quote and special character. Additionally there is a provision of raw string, to treat special characters as normal, by putting r before first quote
    • >>> s= 'first \n second' 
      >>> print(s) 
      >>> s=r'first \n second' 
      >>> print(s) 
      first \n second
    • In C, You need library functions strlen, strcpy,strcat for string manipulation. I found this link very useful .
    • In Python, String can be manipulated similar to numbers. For ex : Add,Copy,comparison(==)etc. You may use built-in functions such as len(),str.islower() for respective operations.
    • Strings can be concatenated using + and repeated using *
    • Two or more string literals put together gets automatically concatenated

Slicing  or Extract a part using positive & negative indexing  

      • mystr[0], mystr[n], mystr[m:n],mystr[:n],mystr[n:],mystr[-1] are as
      • First ,         nth,             m to n,    before n ,  after n,      last
      • mystr[:]  contains  All elements
      • You may inspect  manipulated string using id(mystr) for better understanding the concept

Array vs Lists 

  •  C Array , defined  with square brackets , is a collection of same data types
  •  We have already discussed C Array,Linked List etc at  this earlier
  • C Array is immutable while Python List is mutable i.e can be modified
  • Python List , defined  with square brackets, is an ordered dynamic collection of arbitrary objects separated by commas. It can contain functions and classes as well. It supports concatenation using + and slicing as explained in strings. It also has a number of built-in functions for manipulation
  • One can use help() to get a quick overview of list methods  such as insert(),remove(),pop(),sort() etc. We will discuss alternative method in next blog
  • List can be nested
>>> a=[True,1,5,6,"name","class"]
>>> print(a)
[True, 1, 5, 6, 'name', 'class']
>>> a[0]
>>> a[-1]
>>> b=[8,'marks']
>>> c=a+b
>>> print(c)
[True, 1, 5, 6, 'name', 'class', 8, 'marks']
#Try type(c[0])

 List can be used as stack(LIFO)

>>> stack=[1,2,3,"test",7]
>>> stack
[1, 2, 3, 'test', 7]
>>> stack.append(9)
>>> stack
[1, 2, 3, 'test', 7, 9]
>>> stack.pop()    
>>> stack
[1, 2, 3, 'test', 7]
  •  one can optionally pass item index to pop() for removing particular element, remove() requires the object itself as parameter

 List can also be used as queue(FIFO)

>> from collections import deque
>>> queue=deque(["test",1,5,6"OK",7])
>>> queue
deque(['test', 1, 5, 6, 'OK', 7])
>>> queue.popleft()
>>> queue
deque([1, 5, 6, 'OK', 7])   
#queue.append() will add element to right end
>>> del queue[3]
>>> queue
deque([1, 5, 6, 7])
  • del command shown above can be used for any list


  • Tuples are used for grouping data similar to struct in C but here is no name and access is like array element
  • Tuples can be a set of elements separated by comma(,)
  • Single tuple also must have comma(,) otherwise it will be simple assignment
  • It can be defined with or without parenthesis
  • List is mutable but Tuple is immutable. The  list properties of slicing,nesting and access by square bracket applies to Tuple as well but it can’t be modified.
  • Tuple is faster than List and protects data from modification
>>> a=2,3,"test",4,True
>>> a
(2, 3, 'test', 4, True)
>>> type(a[-1])
<class 'bool'>
>>> a[2:-1]
('test', 4)
>>> a[2:4]
('test', 4)
#Try type(a) 

I have discussed this at   Learn C-Python Video  in detail.

I am trying to improve usability of my  site.If you find something missing, Any feedback in comment box will help to  take corrective action.  Please subscribe to YouTube channel Embedkari  for additional embedded related stuff.

Embedkari  provides  100 mins videos on step-by-step Python uses with Cloud and Machine Learning. These videos are under low cost training and kept at Rs500 only.. Please check the detail  of Low Cost Training Option1.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.