Creating lists from a list

This is the place for queries that don't fit in any of the other categories.

Creating lists from a list

Postby angus1964 » Wed Aug 21, 2013 4:51 pm

Hi

I'm pretty new to python and am working on a project for my local darts association.
I have a list i.e teams = ['ajax', 'bjax', 'snug', 'whalsay']
What i would like to do is create a seperate list for each team from this i.e end up with
ajax = [ ]
bjax=[]
snug=[]
whalsay=[]
I apologise if this is easy and i have just not been able to see it.

Many thanks in advance for any help.

Angus
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: Creating lists from a list

Postby stranac » Wed Aug 21, 2013 5:00 pm

You don't actually want to do that(it would require some nasty code).
You should instead create a dict to keep the lists.

One way to do this is using a dict comprehension:
Code: Select all
team_dict = {name: [] for name in teams}
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: Creating lists from a list

Postby metulburr » Wed Aug 21, 2013 5:02 pm

i would just use a dictionary, unless you need them to be in order:
Code: Select all
teams = {'ajax':[], 'bjax':[], 'snug':[], 'whalsay':[]}

print(teams)
teams['ajax'].append(1)
print(teams)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Creating lists from a list

Postby angus1964 » Wed Aug 21, 2013 5:09 pm

Thanks for the quick replys
Metulburr your dictionary idea seems like a good idea especially after seeing the append as i was planning to run through a fixture list and increase each list accordingly. Only problem there is i would need to be able to print them in a specific order based on one of the fields in the list?

Again help much appreciated

Angus
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: Creating lists from a list

Postby metulburr » Wed Aug 21, 2013 5:21 pm

well first of all, stranac's code does the same:
Code: Select all
teams = ['ajax', 'bjax', 'snug', 'whalsay']
team_dict = {name: [] for name in teams}
print(team_dict)


Only problem there is i would need to be able to print them in a specific order based on one of the fields in the list?

order of the dictionaries values, or order of the dictionary keys? the dict values are lists thus are already ordered. If you mean the dictionary's keys, they cannot be ordered.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Creating lists from a list

Postby angus1964 » Wed Aug 21, 2013 5:28 pm

Thanks again metulburr

Think weve hit a dead end with this. iwas hoping that each list in the dictionary could be expanded.
['ajax', 5, 4,0,1,4]
['bjax'', 5,2,3,0,2]
Then the teams could be sorted into order based on the fourth number, by the looks of what your saying that doesnt seem possible.
Time to back to the drawing board methinks

Angus
angus1964
 
Posts: 14
Joined: Wed Aug 21, 2013 4:42 pm

Re: Creating lists from a list

Postby hansn » Wed Aug 21, 2013 5:34 pm

I would probably implement this using classes. I always prefer to have a name for my values in stead of accessing them simply by list index. I also think it would simplify your sorting issue a lot.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Creating lists from a list

Postby metulburr » Wed Aug 21, 2013 5:44 pm

iwas hoping that each list in the dictionary could be expanded.

it can. any list can be expanded, doesnt matter that it is a value of a dictionary or not.

Code: Select all
['ajax', 5, 4,0,1,4]
['bjax'', 5,2,3,0,2]

i dont follow? If your using a dictionary with the first index of these lists as the keys, why would you need them as the first index of the list?

Then the teams could be sorted into order based on the fourth number, by the looks of what your saying that doesnt seem possible.

this is possible, for example: this creates a list from a dictionary, and sorts it based on its value (the tuple's second index). The dictionaries key becomes the first index of the tuple, whereas the dictionary corresponding value goes to the second index of the same tuple.
Code: Select all
import operator
x = {'test': 2, 3: 4, 4:3, 2:1, 'x':0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))
print(sorted_x)


--output--
Code: Select all
[('x', 0), (2, 1), ('test', 2), (4, 3), (3, 4)]
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Creating lists from a list

Postby micseydel » Wed Aug 21, 2013 6:08 pm

hansn wrote:I would probably implement this using classes. I always prefer to have a name for my values in stead of accessing them simply by list index. I also think it would simplify your sorting issue a lot.

The problem is that people just learning their first programming languages are not in a good position to learn a complex, though awesome, paradigm like OOP. It's an advanced topic that requires as much study if not more than a first language, and goes on top of the knowledge of that language and some experience of problem solving in programming. Besides that, if you've had some experience doing something a more primitive way like is going on here, when you're shown OOP, you can have some appreciate for it.

I'd consider myself an OOP evangelist, but you should definitely be careful about throwing it around. At my alma mater, it's typically a second year course, and for good reason. (I'd argue that might even be a bit soon to really do it right, but time doesn't permit doing it any later than they already do.)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], stranac, Yoriz and 6 guests