Python Forum
Creating a list from a comprehension using a list - 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: Creating a list from a comprehension using a list (/thread-21448.html)



Creating a list from a comprehension using a list - rm197 - Sep-30-2019

keyIDs = list(jsonData.keys())   #list of IDs
listOfValsFromKeys = students[keyIDs[i] for i in range(len(keyIDs))]    #list of names
When I run the lines above, I get a syntax error in between the 'for' and the 'i'. I cannot work out why this is the case. Could someone enlighten me?


RE: Creating a list from a comprehension using a list - stullis - Sep-30-2019

It isn't clear what you're trying to accomplish. students[...] suggests slicing a sequence named "students", but you can't use a list comprehension for that. Please clarify the objective and the data types/structures at play.

Also, line two does not need range(len()). You can just use the iterable in Python.

for i in keyIDs:
    ...



RE: Creating a list from a comprehension using a list - rm197 - Sep-30-2019

(Sep-30-2019, 06:41 PM)stullis Wrote: It isn't clear what you're trying to accomplish. students[...] suggests slicing a sequence named "students", but you can't use a list comprehension for that. Please clarify the objective and the data types/structures at play.

Also, line two does not need range(len()). You can just use the iterable in Python.

for i in keyIDs:
    ...

Thanks, instead of 'students' I was meant to put 'jsonData'. I'm trying to do this with comprehensions instead of using for loops.


RE: Creating a list from a comprehension using a list - stullis - Sep-30-2019

Trying to do what with comprehensions instead of loops. Replacing "students" with "jsonData" will still have the same error. What is the end goal?


RE: Creating a list from a comprehension using a list - rm197 - Sep-30-2019

(Sep-30-2019, 06:46 PM)stullis Wrote: Trying to do what with comprehensions instead of loops. Replacing "students" with "jsonData" will still have the same error. What is the end goal?

keyIDs = list(jsonData.keys())   #list of IDs
listOfValsFromKeys = jsonData[keyIDs[i] for i in range(len(keyIDs))]    #list of names
jsonData is a dictionary that has a key and properties linked to that key. I would like keyIDs to have the keys of the Json (which is working) and listOfValsFromKeys to have the properties linked to these keys. This code currently gives a sytax error but having looked at other examples of comprehensions, I can't see how a syntax error would be given.


RE: Creating a list from a comprehension using a list - stullis - Sep-30-2019

Okay, now I'm tracking. To do that, we need to move jsonData into the comprehension.

keyIDs = list(jsonData.keys())   #list of IDs
listOfValsFromKeys = [jsonData[i] for i in keyIDs]
Of course, you could also use dict.values() instead. It performs the same operation as dict.keys() except it returns the values instead of keys.


RE: Creating a list from a comprehension using a list - rm197 - Sep-30-2019

(Sep-30-2019, 06:54 PM)stullis Wrote: Okay, now I'm tracking. To do that, we need to move jsonData into the comprehension.

keyIDs = list(jsonData.keys())   #list of IDs
listOfValsFromKeys = [jsonData[i] for i in keyIDs]
Of course, you could also use dict.values() instead. It performs the same operation as dict.keys() except it returns the values instead of keys.

Great thanks :)