1

Excuse me for the basic question but I come from a PHP background and made use of multidimensional arrays to transfer information around.

What is the best alternative in Python?

$person[1]['name'] = 'bob'
$person[1]['age'] = 27
$person[1]['email'] = '[email protected]'

What I'm trying to is prepare an array to send to a template, I need to get a list of persons and then calculate when their next meeting/visit should be. I then want to send an array to the template like:

Name   Last Meeting Date     Next Meeting Date
Bob    01/04/2012            01/05/2015

The Next meeting date is calculated based on many variables so I just want to prepare the array in the view and then pass it to the template.

2 Answers 2

1

person = [{'name':'bob', 'age':27, 'email':'[email protected]'}, {...}, {...}]

though if you use django, you might want to check the documentation and how models (user model for instance) work, cause you won't need to declare a list of dicts...

Sign up to request clarification or add additional context in comments.

Comments

0

In Python we have different kinds of data structures, you can't just append data like in the example you show. What you need in this specific case is a list (which is an equivalent of a 1-dimensional array) filled with dicts (just as an array of key-value pairs) where you can define your data:

people = [{"name": "Bob", "last_meeting": yesterday, "next_meeting": tomorrow}, {...}, ...]

Check this link for more info on data structures in python: http://docs.python.org/2/tutorial/datastructures.html

Though, @nnaelle is right, if you are using django and not just python, you should check out how the framework manages requests using Forms, Models, Querysets, views and all the useful stuff that powers Django: https://docs.djangoproject.com/en/1.5/

That will for sure ease a lot of your work ;)

Good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.