Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Creating a list from a comprehension using a list
#1
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?
Quote
#2
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:
    ...
Quote
#3
(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.
Quote
#4
Trying to do what with comprehensions instead of loops. Replacing "students" with "jsonData" will still have the same error. What is the end goal?
Quote
#5
(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.
Quote
#6
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.
Quote
#7
(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 :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Randomly assign values in List 1 to a value in List 2 Seneca260 4 206 9 hours ago
Last Post: buran
  List comprehension. Shor for loop version dervast 1 53 Yesterday, 12:34 PM
Last Post: perfringo
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 88 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  item = index position - list of list RavCOder 9 145 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  List Comprehension - Creating a list of the length of an item help paul41 2 164 Nov-18-2019, 10:21 AM
Last Post: perfringo
  Understanding list comprehension Drone4four 5 117 Nov-15-2019, 04:31 PM
Last Post: ichabod801
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 174 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 146 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Simple list comprehension misunderstanding Mark17 3 220 Oct-10-2019, 07:00 PM
Last Post: buran
  Converting List into list of tuples ARV 4 369 Sep-28-2019, 04:58 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)