Tutorial by Examples

Python lists are zero-indexed, and act like arrays in other languages. lst = [1, 2, 3, 4] lst[0] # 1 lst[1] # 2 Attempting to access an index outside the bounds of the list will raise an IndexError. lst[4] # IndexError: list index out of range Negative indices are interpreted as countin...
Starting with a given list a: a = [1, 2, 3, 4, 5] append(value) – appends a new element to the end of the list. # Append values 6, 7, and 7 to the list a.append(6) a.append(7) a.append(7) # a: [1, 2, 3, 4, 5, 6, 7, 7] # Append another list b = [8, 9] a.append(b) # a: [1, 2, 3, 4, ...
Use len() to get the one-dimensional length of a list. len(['one', 'two']) # returns 2 len(['one', [2, 3], 'four']) # returns 3, not 4 len() also works on strings, dictionaries, and other data structures similar to lists. Note that len() is a built-in function, not a method of a list objec...
Python supports using a for loop directly on a list: my_list = ['foo', 'bar', 'baz'] for item in my_list: print(item) # Output: foo # Output: bar # Output: baz You can also get the position of each item at the same time: for (index, item) in enumerate(my_list): print('The item i...
Python makes it very simple to check whether an item is in a list. Simply use the in operator. lst = ['test', 'twest', 'tweast', 'treast'] 'test' in lst # Out: True 'toast' in lst # Out: False Note: the in operator on sets is asymptotically faster than on lists. If you need to use it ...
You can use the reversed function which returns an iterator to the reversed list: In [3]: rev = reversed(numbers) In [4]: rev Out[4]: [9, 8, 7, 6, 5, 4, 3, 2, 1] Note that the list "numbers" remains unchanged by this operation, and remains in the same order it was originally. To r...
The emptiness of a list is associated to the boolean False, so you don't have to check len(lst) == 0, but just lst or not lst lst = [] if not lst: print("list is empty") # Output: list is empty
The simplest way to concatenate list1 and list2: merged = list1 + list2 zip returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables: alist = ['a1', 'a2', 'a3'] blist = ['b1', 'b2', 'b3'] for a, b in zip(alist, blist):...
You can use all() to determine if all the values in an iterable evaluate to True nums = [1, 1, 0, 1] all(nums) # False chars = ['a', 'b', 'c', 'd'] all(chars) # True Likewise, any() determines if one or more values in an iterable evaluate to True nums = [1, 1, 0, 1] any(nums) # True val...
Removing duplicate values in a list can be done by converting the list to a set (that is an unordered collection of distinct objects). If a list data structure is needed, then the set can be converted back to a list using the function list(): names = ["aixk", "duke", "edik&...
Starting with a three-dimensional list: alist = [[[1,2],[3,4]], [[5,6,7],[8,9,10], [12, 13, 14]]] Accessing items in the list: print(alist[0][0][1]) #2 #Accesses second element in the first list in the first list print(alist[1][1][2]) #10 #Accesses the third element in the second list in...
It's possible to compare lists and other sequences lexicographically using comparison operators. Both operands must be of the same type. [1, 10, 100] < [2, 10, 100] # True, because 1 < 2 [1, 10, 100] < [1, 10, 100] # False, because the lists are equal [1, 10, 100] <= [1, 10, 100] #...
For immutable elements (e.g. None, string literals etc.): my_list = [None] * 10 my_list = ['test'] * 10 For mutable elements, the same construct will result in all elements of the list referring to the same object, for example, for a set: >>> my_list=[{1}] * 10 >>> print(my_...

Page 1 of 1