Python Forum
how does .join work with list and dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how does .join work with list and dictionaries
#1
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]
Reply
#2
.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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
.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
Reply
#4
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.
Reply
#5
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.
Reply
#6
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))
Reply
#7
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,028 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,259 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 765 May-19-2023, 11:19 AM
Last Post: deanhystad
  How to work with list kafka_trial 8 1,941 Jan-24-2023, 01:30 PM
Last Post: jefsummers
  Join dataframes... So simple but I can't work it out! snakes 1 1,341 Oct-27-2021, 09:15 AM
Last Post: snakes
  function that returns a list of dictionaries nostradamus64 2 1,701 May-06-2021, 09:58 PM
Last Post: nostradamus64
  convert List with dictionaries to a single dictionary iamaghost 3 2,805 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,310 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,196 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020