0

I made a dictionary:

sort = {
    str(4213) : ("STEM Center",0),
    str(4201) : ("Foundations Lab",1),
    str(4204) : ("CS Lab",2),
    str(4218) : ("Workshop Room",3),
    str(4205) : ("Tiled Room",4),
    "Out" : ("Outside",5)
}

How do I convert it into a list where each item in the list is a tuple? (using a loop)

ex:

[('4213', 'STEM Center', 0),...]
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
etess
  • 17
  • 2

6 Answers6

4

Easiest way to achieve that is using list comprehension:

sort = {
    str(4213) : ("STEM Center",0),
    str(4201) : ("Foundations Lab",1),
    str(4204) : ("CS Lab",2),
    str(4218) : ("Workshop Room",3),
    str(4205) : ("Tiled Room",4),
    "Out" : ("Outside",5)
}

print([(k, name, count) for k, (name, count) in sort.items()])

Prints:

[('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4218', 'Workshop Room', 3), ('4205', 'Tiled Room', 4), ('Out', 'Outside', 5)]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 5
    You can extend this to work for any number of elements in the value by using `(k, *v) for k, v in sort.items()` – user3483203 Jul 15 '18 at 06:03
2

Use dictionary comprehension to unpack a dict into it's keys/values (k/v) and since your example the values are tuples with a content of index-0 and index-1, and your ouput format desired is to pack it all together, you can accomplish that by calling each index of the tuple-value (v[0], v[1])

[(k,v[0], v[1]) for k, v in sort.items()]
PydPiper
  • 411
  • 5
  • 18
2

You can iterate over the items in the dictionary and join them as tuples:

sort = { str(4213) : ("STEM Center",0), str(4201) : ("Foundations Lab",1), str(4204) : ("CS Lab",2), str(4218) : ("Workshop Room",3), str(4205) : ("Tiled Room",4), "Out" : ("Outside",5) }

[(k,) + v for k, v in sort.items()]

Output:

[('4213', 'STEM Center', 0),
 ('4204', 'CS Lab', 2),
 ('4205', 'Tiled Room', 4),
 ('Out', 'Outside', 5),
 ('4201', 'Foundations Lab', 1),
 ('4218', 'Workshop Room', 3)]
cosmic_inquiry
  • 2,557
  • 11
  • 23
1

Since the question specifically mentions that it should be in a loop (perhaps there is further processing that needs to be done that was taken out), here's what that would look like (modifying @Andrej Kesely's strong approach)

sort_ = { str(4213) : ("STEM Center",0), str(4201) : ("Foundations Lab",1), str(4204) : ("CS Lab",2), str(4218) : ("Workshop Room",3), str(4205) : ("Tiled Room",4), "Out" : ("Outside",5) }

result = []
for key, (name, count) in sort_.items():
    result.append((key, name, count))

I renamed sort to sort_. Even though it isn't in this namespace (sort is a method on lists), I don't like using key words.

[('4213', 'STEM Center', 0),
 ('4201', 'Foundations Lab', 1),
 ('4204', 'CS Lab', 2),
 ('4218', 'Workshop Room', 3),
 ('4205', 'Tiled Room', 4),
 ('Out', 'Outside', 5)]
Zev
  • 3,423
  • 1
  • 20
  • 41
-1
new_list = [(k,sort[k][0],sort[k][1]) for k in sort]
print(new_list)

=> [('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1)]

of course you can do that too

l=[] for k in sort:
l.append((k,sort[k][0],sort[k][1]))

[('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1)]

-1

sorted_list = list(sort.items())

This will give you [('Out', ('Outside', 5)), ('4201', ('Foundations Lab', 1)), ('4204', ('CS Lab', 2)), ('4218', ('Workshop Room', 3)), ('4213', ('STEM Center', 0)), ('4205', ('Tiled Room', 4))]

Rohan Shenoy
  • 805
  • 10
  • 23