visit
range(len(x))
In Python, we generally use a for
loop to iterate over an iterable object. A for
loop in Python uses collection based iteration i.e. Python assigns the next item from an iterable to the loop variable on every iteration. The usual usecase of a for
loop is as follows:
values = ["a", "b", "c"]
for value in values:
print(value)
# a
# b
# c
index = 0
for value in values:
print(index, value)
index += 1
# 0 a
# 1 b
# 2 c
or another common way to do this is by using range(len(x))
:
for index in range(len(values)):
value = values[index]
print(index, value)
# 0 a
# 1 b
# 2 c
However, there is an easier and more pythonic way to iterate over iterable objects by using enumerate()
. It is used in a for
loop almost the same way as you use the usual way, but instead of putting the iterable object directly after in
in the for
loop, or putting using it as range(len(values))
, you put it inside the parentheses of enumerate()
as shown below:
for count, value in enumerate(values):
print(count, value)
# 0 a
# 1 b
# 2 c
We can also define a start
argument for enumerate()
as shown below :
for count, value in enumerate(values, start=1):
print(count, value)
# 1 a
# 2 b
# 3 c
The enumerate()
function gives back two variables:
Just like the loop variables in a for
loop, the loop variables can be named anything, for instance, we can call then index
and value
and they’ll still work. enumerate()
is more efficient than a for
loop as it saves you from the hassle to remember to access the value inside the loop and use it correctly and then also remember to advance the value of the loop variable, it is all handled automatically by Python.
newList = [ expression(element) for element in oldList if condition ]
# Using list comprehension to iterate through loop
List = [character for character in 'HackerNoon']
# Displaying list
print(List)
# Output
# ['H', 'a', 'c', 'k', 'e', 'r', 'N', 'o', 'o', 'n']
sorted()
The Python sorted()
function sorts the elements of an iterable object in a specific order (ascending or descending) and returns them as a sorted list. It can be used to sort a sequence (string, tuple, list) or collection (set, dictionary, frozen set) or any other iterator.
The syntax of the sorted()
function is as follows:
sorted(iterable, key=None, reverse=False)
sorted()
function takes at max three parameters:
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
set_res = set(list_inp)
print("The unique elements of the input list using set():\n")
list_res = (list(set_res))
for item in list_res:
print(item)
The unique elements of the input list using set():
25
75
100
20
12
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("The given list is:", myList)
mygen = (element ** 2 for element in myList if element % 2 == 0)
print("Elements obtained from the generator are:")
for ele in mygen:
print(ele)
The given list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements obtained from the generator are:
4
16
36
64
100
.get()
and .setdefault()
.setdefault()
method allows to set dict[key]=default
if key is not already in dict.
The syntax of .setdefault()
looks like following:
dict.setdefault(key, default=None)
Here’s an example code snippet to understand how to use .setdefault()
:
a_dictionary = {"a": 1, "b": 2, "d": 4}
a_dictionary.setdefault("c", 3)
print(a_dictionary)
{'a': 1, 'b': 2, 'd': 4, 'c': 3}
The same thing can also be achieved by using .get()
method by passing a default value for the key, as you can see below:
a_dictionary = {"a": 1, "b": 2, "d": 4}
print(a_dictionary.get("c", 3))
print(a_dictionary)
3
{'a': 1, 'b': 2, 'd': 4}
collections.Counter
Import collections makes the stuff in collections available as:
import collections
from collections import Counter
import collections
c = collections.Counter('abcdaab')
for letter in 'abcde':
print '%s : %d' % (letter, c[letter])
a : 3
b : 2
c : 1
d : 1
e : 0
As the name “f-string” says, they are string literals that have an f at the beginning and curly braces containing expressions that will be replaced with their values at the runtime and then formatted using the __format__
protocol.
name = "Eric"
age = 74
print(f"Hello, {name}. You are {age}.")
# 'Hello, Eric. You are 74.'
.join()
In Python, we can use the .join()
method to concatenate a list of strings into a single string. The usual syntax for this method looks like below:
'String to insert'.join([List of strings])
It can be used in multiple ways -- if you use the empty string ““
, [List of strings] is simply concatenated, and if you use a comma, a comma-delimited string is created. When the newline character \n
is used, a newline is appended after each string. See the example below:
l = ['aaa', 'bbb', 'ccc']
s = ''.join(l)
print(s)
# aaabbbccc
s = ','.join(l)
print(s)
# aaa,bbb,ccc
s = '-'.join(l)
print(s)
# aaa-bbb-ccc
s = '\n'.join(l)
print(s)
# aaa
# bbb
# ccc
{**d1, **d2}
(Python 3.5+)The easiest way to merge dictionaries is by using the unpacking operator (**
). The syntax for this method looks like this:
{**dict1, **dict2, **dict3}
d1 = {'k1': 1, 'k2': 2}
d2 = {'k3': 3, 'k4': 4}
print({**d1, **d2})
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': 4}
if x in list
colors = ["red", "green", "blue"]
c = "red"
# cumbersome and error-prone
if c == "red" or c == "green" or c == "blue":
print("is main color")
colors = ["red", "green", "blue"]
c = "red"
# better:
if c in colors:
print("is main color")