Re: Easy way to get a list of tuples.
> > > > 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. > > > You have not showed us what you tried, but you are probably missing a pair > of brackets. > > C:\Users\User>python > Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit > (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> x = [] > >>> x.append(('a', 'b', 'c')) > >>> x.append(('p', 'q', 'r')) > >>> x > [('a', 'b', 'c'), ('p', 'q', 'r')] > >>> > > Does this help? > > Frank Millman Oh yes I just had one set of brackets with my append. Thanks Frank -- https://mail.python.org/mailman/listinfo/python-list
Re: Easy way to get a list of tuples.
On 2017-09-21 12:38, Sayth Renshaw wrote: > 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. I suggest you have a very close look at my example code :-) -- Thomas Jollans -- https://mail.python.org/mailman/listinfo/python-list
Re: Easy way to get a list of tuples.
On 21/09/2017 11:18, Sayth Renshaw wrote: Hi I have been toying with json and I particular area where I cannot get the desired result a list of tuples as my return. The json from the API is way to long but I don't think it will matter. .. hitting url data = r.json() for item in data["RaceDay"]['Meetings'][0]['Races']: raceDetails = item['RacingFormGuide']['Event']['Race'] print(raceDetails) This returns {'Number': 1, 'NumberDisplay': '01', 'Distance': 1000, 'DistanceDisplay': '1000 METRES', 'Name': 'CLASS 3 HANDICAP', 'NameForm': 'HIGHWAY-C3'} {'Number': 2, 'NumberDisplay': '02', 'Distance': 1600, 'DistanceDisplay': '1600 METRES', 'Name': 'BM 90 HANDICAP', 'NameForm': 'BM90'} {'Number': 3, 'NumberDisplay': '03', 'Distance': 1100, 'DistanceDisplay': '1100 METRES', 'Name': 'HERITAGE STAKES', 'NameForm': 'HERITAGE'} {'Number': 4, 'NumberDisplay': '04', 'Distance': 1400, 'DistanceDisplay': '1400 METRES', 'Name': 'BILL RITCHIE HANDICAP', 'NameForm': 'RITCHIE'} {'Number': 5, 'NumberDisplay': '05', 'Distance': 1400, 'DistanceDisplay': '1400 METRES', 'Name': 'TEA ROSE STAKES', 'NameForm': 'TEA ROSE'} {'Number': 6, 'NumberDisplay': '06', 'Distance': 1600, 'DistanceDisplay': '1600 METRES', 'Name': 'GEORGE MAIN STAKES', 'NameForm': 'GEO MAIN'} {'Number': 7, 'NumberDisplay': '07', 'Distance': 1100, 'DistanceDisplay': '1100 METRES', 'Name': 'THE SHORTS', 'NameForm': 'THE SHORTS'} {'Number': 8, 'NumberDisplay': '08', 'Distance': 2000, 'DistanceDisplay': '2000 METRES', 'Name': 'KINGTON TOWN STAKES', 'NameForm': 'KING TOWN'} {'Number': 9, 'NumberDisplay': '09', 'Distance': 1200, 'DistanceDisplay': '1200 METRES', 'Name': 'BM 84 HANDICAP', 'NameForm': 'BM84'} My goal is to select a few elements and create a list of 3 element tuples like this [('CLASS 3 HANDICAP', 1, 1000), ('BM 90 HANDICAP', 2, 1600), ('HERITAGE STAKES', 3, 1100), ('BILL RITCHIE HANDICAP', 4, 1400), ('TEA ROSE STAKES', 5, 1400), ('GEORGE MAIN STAKES', 6, 1600), ('THE SHORTS', 7, 1100), ('KINGTON TOWN STAKES', 8, 2000), ('BM 84 HANDICAP', 9, 1200)] I get close creating a list of elements but each attempt I try to create the list of tuples fails. 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) which returns ['CLASS 3 HANDICAP', 1, 1000, 'BM 90 HANDICAP', 2, 1600, 'HERITAGE STAKES', 3, 1100, 'BILL RITCHIE HANDICAP', 4, 1400, 'TEA ROSE STAKES', 5, 1400, 'GEORGE MAIN STAKES', 6, 1600, 'THE SHORTS', 7, 1100, 'KINGTON TOWN STAKES', 8, 2000, 'BM 84 HANDICAP', 9, 1200] How do I get the tuples? Cheers Sayth --- This email has been checked for viruses by AVG. http://www.avg.com After a quick glance and hence completely untested:- raceData.append((raceDetails['Name'],raceDetails['Number'],raceDetails['Distance'])) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Easy way to get a list of tuples.
"Sayth Renshaw" wrote in message news:cd4aa5c7-47ee-442b-945e-490b0674e...@googlegroups.com... 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. You have not showed us what you tried, but you are probably missing a pair of brackets. C:\Users\User>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. x = [] x.append(('a', 'b', 'c')) x.append(('p', 'q', 'r')) x [('a', 'b', 'c'), ('p', 'q', 'r')] Does this help? Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: Easy way to get a list of tuples.
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 "", line 1, in > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Easy way to get a list of tuples.
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 "", line 1, in 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 -- https://mail.python.org/mailman/listinfo/python-list