Easy way to get a list of tuples.

Thomas Jollans tjol at tjol.eu
Thu Sep 21 06:31:13 EDT 2017


On 2017-09-21 12:18, Sayth Renshaw wrote:
> This is my closest code
> 
> data = r.json()
> 
> raceData = []
> 
> for item in data["RaceDay"]['Meetings'][0]['Races']:
>     raceDetails = item['RacingFormGuide']['Event']['Race']
>     raceData += (raceDetails['Name'],raceDetails['Number'],raceDetails['Distance'])
> 
> print(raceDetails)
> 

You're close!

The operator += extends a list with the items of another sequence (or
iterable). What you're looking for is the method .append(), which adds a
single element.

Observe:

Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
py> a_list = []
py> a_list += 1,2,3
py> a_list
[1, 2, 3]
py> a_list.append(4)
py> a_list
[1, 2, 3, 4]
py> a_list += 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
py> a_list.append((5,6,7))
py> a_list
[1, 2, 3, 4, (5, 6, 7)]
py>


-- 
Thomas Jollans



More information about the Python-list mailing list