python list type

list type is mutable sequence type.

I will introduce how to make list object in this page.
there are some ways to make list object.


  • using square brackets with empty between the brackets
the expression is like below


var = []

if with empty brackets, empty list will be made.


  • using a list comprehension.
the expression is like below


var = [x for x in iterable]

"iterable" means that something like sequence types. and technically, something that implement "__next__" method. I will introduce iterable and iterator type in the next subject. so, in this page, it will be fine to think it of like sequence type.

back to the main subject, you can make list items using "for" expression. the syntax is like above example code.



  • using the type constructor.

everything is object in python. it means list is also object that has type constructor. as a result, you can make the list object using type constructor. example is like below.


a = list()
b = list(iterable)

if you don't pass any parameter, empty list will be made. if you pass iterable to the parameter, list will be made using the iterable.


let's check them in example code.



test_iterable  = (1,2,3,4,5) #tuple type is also iterable type.

list1 = []
print(list1)

list2 = [x*x for x in test_iterable]
print(list2)

list3 = list()
print(list3)

list4 = list(test_iterable)
print(list4)







Comments

Popular posts from this blog

making menubar and menu on QML

let's start QML programming with PySide2

QSizePolicy Fixed example in pyside2