Python Forum
how does .join work with list and dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how does .join work with list and dictionaries (/thread-27895.html)



how does .join work with list and dictionaries - gr3yali3n - Jun-26-2020

location={
	0:'quit',
	1:'town',
	2:'beach',
	3:'country side',
	4:'city',
	5:'outer space'
}

exits=[
	{'Q':0},
	{'W':2,'E':3,'N':5,'S':4,'Q':0},
	{'N':5,'Q':0},
	{'W':1,'Q':0 },
	{'N':1,'W':2,'Q':0},
	{'W':2,'S':1,'Q':0}
]

loc = 1 
while True:
	avail_exits= ','.join(exits[loc].keys())
	print(location[loc])
	if loc ==0:
		break
	
	direction=input('available exits are ' + avail_exits)
	print()
	if direction in exits[loc]:
		loc=exits[loc][direction]
	else:
		print('you cant go that direction')
So, I am taking a python class on udemy, I felt confident enough to move on to the next lecture and here is the code that is the topic, slightly different, but the code is on point.
I understand most of it thanks to this forum and said class but I need to understand
What is going on with .join(exits[loc].keys())
How is this portion of code working?
The same thing with
Loc = exits[loc][direction]


RE: how does .join work with list and dictionaries - DPaul - Jun-26-2020

.join() makes a concatenated string of an iterable, in your case of a dictionary at place [loc] in a list of dictionaries.
The elements are separated by a ','.join , comma in your example.
This is done to present the list of choices in a nicer format.
exits[loc][direction] : says that you need to take the dictionary number [loc] and specifically the value with key [direction]
Paul


RE: how does .join work with list and dictionaries - hussainmujtaba - Jun-26-2020

.join() actually is used to join strings.So let us focus on your code.The code
exits=[
    {'Q':0},
    {'W':2,'E':3,'N':5,'S':4,'Q':0},
    {'N':5,'Q':0},
    {'W':1,'Q':0 },
    {'N':1,'W':2,'Q':0},
    {'W':2,'S':1,'Q':0}
]
loc = 1 
avail_exits= ','.join(exits[loc].keys())
print(avail_exits)
output:
Output:
E,S,N,Q,W
So after using, the join statement made a string of all keys at location 1 and separated them by ',', which we can change too to anything.For example to keep just space between them we can use
avail_exits= ' '.join(exits[loc].keys())
print(avail_exits
Output:
E S N Q W
Hope you got how join works


RE: how does .join work with list and dictionaries - gr3yali3n - Jun-29-2020

Yes, I understand how the join method works now.
I still feel uncertain on how the code is working.
For example
If direction in exits[loc]:
   loc=exits[loc][dir]
How does exits[loc][dir] have the position of player?
Loc = location number
Exits = north west etc. plus corresponding loc numbers
Direction = user input

So how does
Loc = exits[loc][direction]
Actually work?

I’m sorry that I need everything spelled out in crayon right now.


I need to go back and review a few things before moving on.


RE: how does .join work with list and dictionaries - ndc85430 - Jun-30-2020

This shouldn't be hard:

exits[loc] gives you the value in the list exits at the index specified by loc. That value is a dictionary, so exits[loc][direction] gives you the value in that dictionary associated with the key specified by direction.


RE: how does .join work with list and dictionaries - gr3yali3n - Jul-07-2020

But I’m still off on as to why I’m thinking .join() will place the additional string at the end\after the original string instead of one letter at a time while repeating the string?

Like in
string='a turtle sits on a log'
string2='watching the sky'

print(string.join(string2))



RE: how does .join work with list and dictionaries - menator01 - Jul-07-2020

string='a turtle sits on a log'
string2='watching the sky'

print(str.join(' ',(string, string2)))
or
string='a turtle sits on a log'
string2='watching the sky'

print(' '.join((string, string2)))
Outputs in the order entered.
Output:
a turtle sits on a log watching the sky



RE: how does .join work with list and dictionaries - bowlofred - Jul-07-2020

join() takes 2 things: an iterator of items, and a string to put between the items. If you hand it a list in the iterator place, it joins the elements with the string. If you hand it a string in the iterator place (as in your example), it joins the individual characters with the other string.

If you want to slap some strings together with join(), put them in a list and join them with a space or an empty string.

string='a turtle sits on a log'
string2='watching the sky'

print(" ".join([string, string2]))
Output:
a turtle sits on a log watching the sky