0

learning python and have come up with a question about something I'm trying to do. Earlier in the program I've created a series of lists through looping and adding elements if they meet some condition after some calculations. As an example these are:

v0 = [2110, 2110]
v1 = [2270, 2297, 2206, 2075]
v2 = [2220, 2024, 2203, 2022]
v3 = [2052, 2101, 2150, 2053]
v4 = [2079, 2609, 2065, 2082]

Now that I've generated these, I want to make a list of lists that is later converted into a dataframe for the type of bar plot I want to create.

so I've tried this:

i = 0
for i in xrange(B): #where B is = 5
    A1.append("v"+str(i))

which yields:

In[21]:  A1
Out[21]: ['v0', 'v1', 'v2', 'v3', 'v4']

and what I really want is:

A1 = [v0,v1,v2,v3,v4]

A1
Out[26]: [[2110, 2110],[2276, 2277, 2276, 2275],[2220, 2024, 2203, 2022],[2052, 2101, 2150,2053],              [2079, 2609, 2065, 2082]]

I need to be able to iterate over the lists since they can always be different in number and length. Any suggestions on how to access the list contents in the list of lists are very much appreciated!

velodrome
  • 216
  • 3
  • 5
  • The better way is using a dictionary : for problems like this ! so for that you need to put your lists in a list like `L` and with the below code create a dictionary : >>> {"v"+str(i):j for i,j in enumerate(L)} {'v0': [2110, 2110], 'v1': [2270, 2297, 2206, 2075], 'v2': [2220, 2024, 2203, 2022], 'v3': [2052, 2101, 2150, 2053], 'v4': [2079, 2609, 2065, 2082]} – Mazdak Oct 20 '14 at 18:45
  • Thanks Karsa, looks like I need to read up on using dictionaries in python and try out your code block. Thanks for your input! :-) – velodrome Oct 20 '14 at 18:58
  • yes i think so , your welcome ! – Mazdak Oct 20 '14 at 19:04

0 Answers0