Python Lists


Lists

The Python list, is a sequence of values, separated by commas between square brackets. Lists can have values of different data types.

Example

Extract all values from a list with python

>>> my_list = [1, 5, 7, 21, 33]
>>> my_list
[1, 5, 7, 21, 33]

Extract value from position 0 and 2 from the list

>>> my_list = [1, 3, 9, 17, 22]
>>> my_list [0]
1
>>> my_list [2]
9

Extract value from negative position from the list

>>> test = [1, 2, 3]
>>> test[-1]
3		

Add values in the lists

>>> test = [1, 2, 3]
>>> test + [4, 5]
[1, 2, 3, 4, 5]

Add operation between lists

>>> a = [1, 2]
>>> b = [1, 3]
>>> a + b
[1, 2, 1, 3]

Lists concatenate

>>> x = [1, 2]
>>> y = ['a', 'b']
>>> x + y
[1, 2, 'a', 'b']