Python List - Quick Reference

Python List - Quick Reference

Just remember list[start:end:-steps]. That’s all!

There are many people who misinterpret above quick reference diagram and apply it on string. So Let me tell you how not to use it.

1
2
3
4
5
6
7
8
9
word = "SOLOTHOUGH"
print(word[0:4])      #SOLO
print(word[-0:4])     #SOLO
print(word[-0:-6])    #SOLO

print(word[4:6])      #TH

#word.append("T")     #Error: no 'append' attribute
#print(word)          #Error

But how to use it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
list = ["S","O","L","O","T","H","O","U","G","H"]
print(list[0:4])      #['S', 'O', 'L', 'O']
print(list[-0:4])     #['S', 'O', 'L', 'O']
print(list[-0:-6])    #['S', 'O', 'L', 'O']

print(list[4:6])      #['T', 'H']

list.append("T")
print(list)           #['S', 'O', 'L', 'O', 'T', 'H', 'O', 'U', 'G', 'H', 'T']

list = list + [".","C","O","M"]
print(list)           #['S', 'O', 'L', 'O', 'T', 'H', 'O', 'U', 'G', 'H', 'T', '.', 'C', 'O', 'M']

#Unpacking
platform = ['S', 'O', 'L', 'O', '💭']
other = [*platform]   #supported by tuple, set
another = platform[:] #supported by tuple but not set

#dictionary
about = { 
  "feature" : "simplified",
  "aim"     : "another perspective"
}

{**about}

Compare

Python supports different similar data structure. And you should understand the difference.

  • Lists are editable, indexed, ordered
  • Tuples are read only lists
  • Set are like set :) You can say unique list. And not ordered.
  • Dictionary is like json or map or object

You can store different data types in any of above DS. And you can have nested DS as well.

solothought python list set tuple dictionary quick comparison

Quiz

Let’s check if you really understand it. Remember that a question may have multiple answers.


I hope now it is pretty clear to you. If you still have any doubt then let’s catch up on the discussion page.



Your feedback is really important. So I can improve my future articles and correct what I have already published. You can also suggest if you want me to write an article on particular topic. If you really like this blog, or article, please share with others. That's the only way I can buy time to write more articles.

Amit
Amit Author & Maintainer of many opensourse projects like fast-xml-parser, imglab, stubmatic etc. Very much interested in research & innovations.

Related Articles

Visual Explanation of Python NumPy Library
Python Method - Quick Reference
Visual Explanation of Python Panda Library