Why does iterating through a dictionary and adding it to a string split up the string?

Question:
Why does iterating through a dictionary and adding it to a string split up the string?

list = []

dict = {
    "ab": 1,
    "bc": 2,
    "ca": 3,
}

for i in dict:
    list += i

for i in list:
    print(i)

Where is string defined?

2 Likes

Could you please clarify what output you are expecting? And why you’re expecting it? (Also what Qwerty requested)

From my POV that code is executing exactly as it should in this scenario. (Adds each item in the dict to the string, which then becomes “abbcca”)

2 Likes

What you want here is

string = "\n".join(dict)

except you might not want to use dict as a variable name because it’s a built-in function.

2 Likes
list = []

dict = {
    "ab": 1,
    "bc": 2,
    "ca": 3,
}

for i in dict:
    list += i

for i in list:
    print(i)


Here’s a more accurate representation of my code and what i’m trying to do. Sorry for any confusion on my previous example.

What about this?

list = []

dict = {
    "ab": 1,
    "bc": 2,
    "ca": 3,
}

ind = 0
for i in dict:
    list[ind] = i
    ind+=1

for i in list:
    print(i)

While it might be bad practice, it should do what you want I think.
Edit: better practice?

testList = []

testDict = {
    "ab": 1,
    "bc": 2,
    "ca": 3,
}

def dictToList(_dict):
  i = 0
  returnList = []
  for item in _dict:
    returnList[i] = item
  return returnList

testList = dictToList(testDict)

for i in testList:
    print(i)

dict is a function? Thought it was a class

This just throws an index error.

Ok well now your title doesn’t make sense. What’s the expected and actual result now?

1 Like

Hmm. How about my “better practice?” one?

Still throws an index error.

Hmm. I’ll see what I can come up with then.

the list should look like this:
[“ab”, “bc”, “ca”]

what is looks like is this:
[“a”, “b”, “b”, “c”, “c”, “a”]

Hang on, I would like to know why you are getting the “keys” for the dicts, and saving those to a list.

I want to print each key out.

Fair enough I suppose.

for key in dict:
  print(key)

yea that works but i need them in list format. I don’t want to change a bunch of code just so I can print a bunch of keys in a dict

How about this one?

testList = []

testDict = {
    "ab": 1,
    "bc": 2,
    "ca": 3,
}

def dictToList(dictionary: dict) -> list:
  returnList = []
  for key in dictionary:
    returnList.append(key)
  return returnList

testList = dictToList(testDict)

for i in testList:
    print(i)

Also, if you want both lists:

testDict = {
    "ab": 1,
    "bc": 2,
    "ca": 3,
}

def dictToList(dictionary: dict):
  keys = []
  values = []
  for key in dictionary:
    keys.append(key)
    values.append(dictionary[key])
  return keys, values

testKeys, testValues = dictToList(testDict)

for key in testKeys:
  print(key)
for value in testValues:
  print(value)
4 Likes

I’m not sure putting an underscore before the variable is any better than just the name itself. Maybe dictToConvert instead? Or dictionary?