Python Forum
why tuple instead of list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why tuple instead of list
#6
I said namedtuple, not tuple. For example

population.csv
Output:
name,state,population New York City,NY,8601186 Los Angeles,CA,4057841 Chicago,IL,2679044 Houston,TX,2359480 Phoenix,AZ,1711356 Philadelphia,PA,1576596 San Antonio,TX,1565929 San Diego,CA,1453775
Compare 4 snippets

import csv
from collections import namedtuple


with open('population.csv') as f:
    next(f) # skip header
    for line in f:
        line = line.strip().split(',')
        print(f'Population of {line[0]}, {line[1]} is {line[2]}')

with open('population.csv', newline='') as f:
    rdr = csv.reader(f)
    next(rdr) # skip header
    for line in rdr:
        print(f'Population of {line[0]}, {line[1]} is {line[2]}')

with open('population.csv', newline='') as f:
    rdr = csv.DictReader(f)
    for city in rdr:
        print(f"Population of {city['name']}, {city['state']} is {city['population']}")


City = namedtuple('City', 'name state population')
with open('population.csv', newline='') as f:
    rdr = csv.DictReader(f)
    for record in rdr:
        city = City(**record)
        print(f"Population of {city.name}, {city.state} is {city.population}")
This is very basic example (assuming no suitable custom class to use), but you get the idea. Imagine, you are not just printing elements.
In the last two snippets, if you just print city, the namedtuple is more readble than dhe dict
Output:
OrderedDict([('name', 'Los Angeles'), ('state', 'CA'), ('population', '4057841')])
vs
Output:
City(name='New York City', state='NY', population='8601186')
There is discussion about enhancement to csv module, to have a reader that returns nameduples, not dicts.

https://bugs.python.org/issue1818

But not sure what the current state is

Also instead of returning list/tuple from function, it is better to return namedtuple instead

Overall, using namedtuple instead of just tuple/list makes the element access and code readability better
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
why tuple instead of list - by Skaperen - Feb-14-2020, 11:50 PM
RE: why tuple insyead of list - by ndc85430 - Feb-15-2020, 10:31 AM
RE: why tuple insyead of list - by Skaperen - Feb-16-2020, 08:15 AM
RE: why tuple insyead of list - by buran - Feb-16-2020, 10:49 AM
RE: why tuple insyead of list - by Skaperen - Feb-17-2020, 12:12 AM
RE: why tuple insyead of list - by buran - Feb-17-2020, 08:22 AM
RE: why tuple instead of list - by DeaD_EyE - Feb-17-2020, 09:47 AM
RE: why tuple instead of list - by buran - Feb-17-2020, 09:56 AM
RE: why tuple instead of list - by Skaperen - Feb-18-2020, 01:00 AM
RE: why tuple instead of list - by DeaD_EyE - Feb-18-2020, 09:18 AM
RE: why tuple instead of list - by Skaperen - Feb-18-2020, 06:37 PM
RE: why tuple instead of list - by perfringo - Feb-18-2020, 08:15 PM
RE: why tuple instead of list - by Skaperen - Feb-19-2020, 02:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  strange difference between list() and tuple() using iter() Skaperen 1 1,790 Nov-01-2020, 06:49 AM
Last Post: buran

Forum Jump:

User Panel Messages

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