Despite what Guido says, dict(x, **y)
is in line with the dict specification, which works for both Python 2 and 3. The fact that this only works for string keys is a direct consequence of how keyword parameters work, not a shortcoming of dict
. However, using the **
operator in this place is not an abuse of the mechanism, in fact, **
was designed precisely to pass dictionaries as keywords.
Combining two dictionaries:
dict(D1, **D2)
Combining multiple dictionaries:
{**D1, **D2, **D3}
Refer to the python code:
D1={"A":1,"B":2} D2={"C":3,"D":4} d3={"E":1,"A to E":11} print("two dict merge") print(dict(D1, **D2)) print("multiple dictionaries merge") print({**D1,**D2,**d3})
string.join()
" ".join()
allows us to concatenate the strings of the same list:
Arr=['hello','learn','python,
'with','FUN']
" ".join(Arr)
Let’s take a look into the Python code:
Arr=['hello','learn','python', "with","fun"] print("before joining the array:\n",Arr) k=" ".join(Arr) print('after joining :\n',k) #concat with diff spl characters k1="-".join(Arr) k2="$".join(Arr) print( k1,"\n",k2)
Instead of initializing the elements of a list (as shown below),
list=[11,12,13,14,145,12,127]
we can simply use range()
in list.
Let’s look at the Python code below:
print("\t Range Into List") K=list(range(11,20)) print(K)
L=[[10],[100],[1000],[10000]]
L1=sum(L,[])
print(L1)
L=[[10],[100],[1000],[10000]] print("the nested list:",L) L1=sum(L,[]) print("after extracting it:",L1)
RELATED TAGS
CONTRIBUTOR
View all Courses