Easy way to get a list of tuples.
Sayth Renshaw
flebber.crue at gmail.com
Thu Sep 21 06:38:17 EDT 2017
On Thursday, 21 September 2017 20:31:28 UTC+10, Thomas Jollans wrote:
> 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
Thanks Thomas yes you are right with append. I have tried it but just can't get it yet as append takes only 1 argument and I wish to give it 3.
I am really having trouble creating the groups of 3, since I am getting one consistent stream.
Cheers
Sayth
More information about the Python-list
mailing list