Python Forum
Creating a list from a comprehension using a list
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?
Reply
#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:
    ...
Reply
#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.
Reply
#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?
Reply
#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.
Reply
#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.
Reply
#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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,564 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,500 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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