Delete non alphanumeric characters in Python
The following shows how to delete non-alphanumeric characters in a Python string.
Sort a list of tuples by the first or second element in Python
In Python, `sorted()` sorts a list of tuples by the first element. python s = [('b', 1), ('a', 3), ('c', 2)] s1 = sorted(s) s2 = sorted...
Get a random element from a list in Python
To select a random element of a Python [list](/python-list), use the `random.choice()`. python import random s1 = [1, 2, 3, 4, 5, 6] s2...
Convert a list to json in Python
You can convert a list to a JSON string. python import json s = [1, 2, 3, 'a'] j = json.dumps(s) print(j) # [1, 2, 3, "a"] print(t...
Get the index of the max value in a Python list
Here is an example of finding the index of the max value in a Python list. python s = [5, 6, 7, 4, 3] t = [3, 4, 4, 1] i = s.index(max(...
Python list - Insert the object at the first position
You can insert the element at the first position in the list. python a = ['Apple', 'Microsoft', 'Amazon', 'Google', 'Facebook', 'NVIDIA'] ...
Get the last element of the list in Python
You can get the last element of a list by -1 index. python a = ['car', 'train', 'plane', 'rocket'] b = a[-1] print(b) # rocket ...
Python List (Iterate, append, reverse, pop, etc)
A list is an ordered set of objects in Python. python a = [1, 2, 3] ## Iterate python a = ['com', 'org', 'net'] for i in a: ...
Python zip - Iterate lists in parallel
You can iterate lists in parallel using zip in Python. python a = ['Apple', 'Microsoft', 'Google'] b = ['Mac', 'Windows', 'Android'] fo...
List pop (Python) - How to delete or remove from list by index
`pop` removes the element from a list in Python. `pop(1)` means deleting the second item from a list. python x = ['a', 'b', 'c', 'd'] x....
Concatenate lists in Python (extend, append, addition operator, slicing)
A Python list can be extended by `extend()`, a list method. python x = ['a', 'b', 'c'] y = [1, 2] x.extend(y) print(x) # ['a', 'b',...
Iterate a Python list and get the index and value
You can get indices and values of a [list](python-list) in for loop using Python built-in `enumerate()`. python a = ['sun', 'earth', 'moon'...
Python join - list to string with space or comma
Python string has `join()` method that concatenates all the elements of a Python list. python x = ['Python', 'Java', 'C'] y1 = '-'.join(...
Python cartesian product: Advanced example of list comprehension
The code shows how to make a cartesian product in Python. This is an advanced example of Python list comprehension. python u = ['A', 'B'] ...
Python list comprehension - What does [x + 1 for x in a] mean?
Python list comprehension is to make a list from a list. python a = [1, 2, 3] b = [x + 1 for x in a] print(b) # [2, 3, 4] b is...
Python list to tuple
You can convert a list to tuple by `tuple`. python a = [1, 2, 'Apple'] t = tuple(a) print(t) # (1, 2, 'Apple')
Python list del: How to remove or delete an element from a list
Python built-in function `del` deletes the element of a list. python a = ['Book', 'Note', 'Pen'] del a[1] print(a) # ['Book', 'Pen']...
Python list clear: Remove all the elements from a list
All the elements of a Python list are removed by `clear` method. python a = [1, 2, 3] a.clear() print(a) # [] After cleared, a...
Python Generator - How to create or iterate a generator
The Python generator (expression) is declared on the fly as follows: python a = [1, 3, 5] g = (i * i for i in a) print(g) #
Python empty list - How to check if a list is empty
Python list may be empty and there are many ways to check if a list is empty or not. The below code follows the standard Python coding conventio...
Python range - Create the arithmetic progression in Python
In Python, `range` makes an arithmetic sequence similar to a list. python a = range(4) for i in a: print(i) # 0 # 1 # 2 # 3 ...
Python count function - Get the number of item in a list
You can get the occurrence of an item in a list. python p = ['a', 'a', 'a', 'b', 'c', 'c'] i = p.count('a') print(i) # 3 `coun...
Get index of list item in Python - And get indexes by list comprehension
Python list has `index` method that returns the index of an item. python a = ['book', 'car', 'pen'] i = a.index('book') j = a.index('pe...
Python List Shuffle
You can shuffle Python list by importing `random` and using `shuffle`. python import random a = [1, 2, 3] random.shuffle(a) print(...
Python list - sort by ascending or descending
A Python list can be sorted by using `sort()`. python a = [2, 3, 1] a.sort() print(a) # [1, 2, 3] The default order is ascendi...
Reverse a Python list by negative step slicing
A Python [list](/python-list) can be reverses by `reverse()`. python a = ['Book', 'Note', 'Pen'] a.reverse() print(a) # ['Pen', 'Not...
Python List Append - And what's the difference of the append and extend?
You can append an object to the [list](/python-list) using `append` method. python a = [5, 6, 7] a.append(8) print(a) # [5, 6, 7, 8] ...
Python String
Python List
Python Tuple
Python Set
Python Dictionary
Python Float
Python Decimal
Python Fraction
Python Date & Time
Python Function
Python Class
Python File
Python Built in
Python Math
Python Counter
Python Functools
Python Itertools
Python Deque
Python Tips
© Rollpie