for-loops and dictionaries [duplicate] - python

This question already has answers here:
How are tuples unpacked in for loops?
(8 answers)
How can I add new keys to a dictionary?
(19 answers)
Closed 5 years ago.
Trying to relearn python, working with Introducing Python by Bill Lubanovic. While learning about dictionaries, he uses this for loop to reverse the dictionary e2f to f2e:
e2f = {'dog': 'chien', 'cat': 'chat', 'walrus': 'morse'}
f2e = {}
for english, french in e2f.items():
f2e[french] = english
I really don't understand these for loops, we never defined french or english, and I don't know how this process reverses the dictionary. If someone could explain this for loop it would be greatly appreciated!

Related

creating a list from a set modifies the order of the numbers [duplicate]

This question already has answers here:
python order of elements in set
(2 answers)
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 26 days ago.
I have a list a:
a={4941, 4980, 3855, 4763, 4955}
I convert into a list:
b=list(a)
b now becomes [4980, 4955, 4763, 4941, 3855]
Sorry am I missing something? Eventhough set is an unordered structure why should it change the order ? Or is it the list function that is doing this?
Sorry if this is too naive a question! Many thanks.

How to use the concept of indexing dictionary in python [duplicate]

This question already has answers here:
Append a dictionary to a dictionary [duplicate]
(7 answers)
How to index into a dictionary?
(11 answers)
Closed 2 months ago.
I encountered a program where I had to append one dictionary into another, and I was unable to do it because I could not use indexing in dictionary so I wanted to know what the possible ways are I could do it

Unbracketing python array segments [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 8 months ago.
A=["asd","jkl","qwe"]
[A[1:3],A[0:1]]
gives
[['jkl', 'qwe'], ['asd']]
I wish it just gave
['jkl', 'qwe', 'asd']
How do I accomplish this seemingly elusive task?
edit: the version of python I must work with does not allow for * symbol.
A=["asd","jkl","qwe"]
A = [*A[1:3], *A[0:1]]

Can somebody explain this piece of python code to me? [duplicate]

This question already has answers here:
Python for-in loop preceded by a variable [duplicate]
(5 answers)
Explanation of how nested list comprehension works?
(11 answers)
Closed 4 years ago.
I am trying to figure out what the subsequent lines of python code actually do:
if (var1 and var1) in [ctl for key, value in list(uof.items()) for ctl, com in list(cd.items()) if com == 'spain']:
my_var= uof_map[var1 ]
I assume it executes some kind of the following logic:
for key, values in list(uof.items()):
for ctl, com in list(values.items()):
if com == 'spain':
But apparently the results do differ. Can someone please point me into the right direction?
Notes:
uof is a dictionary of dictionaries
value is a dictionary

Slicing complex dictionary in python [duplicate]

This question already has answers here:
Iterating over dictionaries using 'for' loops
(15 answers)
Closed 5 years ago.
I want to be able to print out just a single character from this dictionary but I haven't been able to figure out the syntax or find it anywhere. Is there a way to do this in vanilla Python 2.7.x given the code below?
dct = {"c":[["1","1","0"],["0","0","0"]], "d":[["1","1","0"],["1","0","0"]],}
for x in dct:
print [x][0][0]
I want the output to be: 11
Any help is much appreciated!
for x in dct:
print(dct[x][0][0])

Categories