newbie question - python lists

L

lee

hi,

rfids= ['01','02']
i = 01
row = {}
items = []
for rfid in rfids:

brains = ['1','2']

if brains:
for brain in brains:
# this loop must run only once for each value of i
row['itemnos'] = 'item_0'+str(i)+'s'
print 'hi'
items.append(row)
print items
break
i=i+1

the above code produces output a:
[{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
but i want it to be,
[{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]

can anyone point wer am erroring.
Thanks in advance.
 
J

Jim

can anyone point wer am erroring.

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop? I think
you are mixing i and brain somehow.

Jim
 
J

Jon Clements

hi,

rfids= ['01','02']
i = 01
row = {}
items = []
for rfid in rfids:

    brains = ['1','2']

    if brains:
        for brain in brains:
            # this loop must run only once for each value of i
            row['itemnos'] = 'item_0'+str(i)+'s'
            print 'hi'
            items.append(row)
            print items
            break
    i=i+1

the above code produces output a:
[{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
but i want it to be,
[{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]

can anyone point wer am erroring.
Thanks in advance.

You've made a good effort (and discovered at least one major common
gotcha).

There's lots in your code that needs highlighting. However, probably a
good starting point would to be describe in plain english, what you're
trying to achieve; Are you really sure you want a list of single item
dict's?

Jon.
 
R

Robert P. J. Day

hi,

rfids= ['01','02']
i = 01

for what it's worth, that statement above isn't even legal python3.
row = {}
items = []
for rfid in rfids:

brains = ['1','2']

if brains:
for brain in brains:
# this loop must run only once for each value of i
row['itemnos'] = 'item_0'+str(i)+'s'
print 'hi'
items.append(row)
print items
break
i=i+1

rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
 
L

lee

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.
 
L

lee

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.
 
L

lee

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.
 
E

Emily Rodgers

lee said:
hi,

rfids= ['01','02']
i = 01
row = {}
items = []
for rfid in rfids:

brains = ['1','2']

if brains:
for brain in brains:
# this loop must run only once for each value of i
row['itemnos'] = 'item_0'+str(i)+'s'
print 'hi'
items.append(row)
print items
break
i=i+1

the above code produces output a:
[{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
but i want it to be,
[{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]

can anyone point wer am erroring.
Thanks in advance.

Hi,

As some others have pointed out, it is not totally clear from your code what
you are trying to do, but I will forgive you for that because you are
clearly not used to python!

I think what you are trying to do is:

items = []
rfids= ['01','02']
for rfid in rfids:
items.append({'itemnos': 'item_%ss' % rfid})
print items

This can also be done using a list comprehension which is one of the (many)
most useful features of python (imo):
rfids= ['01','02']
print [{'itemnos': 'item_%ss' % rfid} for rfid in rfids]

Or on one line (although it is always to be clearer rather trying to fit
stuff into one line - it is not perl!)

print [{'itemnos': 'item_%ss' % rfid} for rfid in ['01', '02']]

Hope this helps.

Emily
 
R

Robert P. J. Day

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

this seems to work:

items = []
brains = ['1','2','3','4']
for brain in brains:
items.append({'item':brain})
print(items)
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]
[{'item': '1'}, {'item': '2'}, {'item': '3'}]
[{'item': '1'}, {'item': '2'}, {'item': '3'}, {'item': '4'}]


rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
 
B

Benjamin Kaplan

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
   row['item'] = brain
   items.append(row)
   print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.

With Python, you're always dealing with objects, not with values. It
isn't apparent with immutable objects like strings and ints, but
here's what's actually happening. At the end, your list is [row, row].
It's the exact same dictionary. The value wasn't copied in. Try this

row = {}
row['item'] = 1

items = [row]
row['item'] = 2
print row

You get the same thing- you're changing the dict that's already in
items. In order to get two different elements, you have to use 2
different dicts.

if brains:
for brain in brains:
# this loop must run only once for each value of i
row = { 'itemnos': 'item_0'+str(i)+'s'}
print 'hi'
items.append(row)
print items
break
i=i+1

 
R

Rami Chowdhury

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

Lee,

When you do
row['item'] = brain
you are actually modifying the existing dictionary object called 'row' --
and I get the impression this is not what you want to do.

I'd suggest creating a new dictionary on each pass, which should give you
the desired behavior.
 
L

lee

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

ok let me make it clear,

brains = ['1','2']
for brain in brains:
    row['item'] = brain
    items.append(row)
    print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.

i got it solved ,

items = []
for brain in brains:
dict = {'itemnos': 'item_0' + brain + 's'}
items.append(dict)
print(items)

thanks all.
Lee.
 
R

Rami Chowdhury

On Nov 6, 9:15 am, lee <[email protected]> wrote:
can anyone point wer am erroring.
I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.

ok let me make it clear,

brains = ['1','2']
for brain in brains:
    row['item'] = brain
    items.append(row)
    print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.

i got it solved ,

items = []
for brain in brains:
dict = {'itemnos': 'item_0' + brain + 's'}
items.append(dict)
print(items)

Glad to see you solved it! A couple of minor considerations:
- I would suggest you use a variable name other than 'dict', as that
shadows the built-in 'dict' type
- I would suggest using the idiom 'item_0%ss' % brain rather than
'item_0' + brain + 's', in case at some point you want to change the type
of 'brain'
 
L

lee

can anyone point wer am erroring.
I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop?  I think
you are mixing i and brain somehow.
Jim
ok let me make it clear,
brains = ['1','2']
for brain in brains:
    row['item'] = brain
    items.append(row)
    print items
This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]
if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .
Thanks
Lee.
i got it solved ,
items = []
for brain in brains:
       dict = {'itemnos': 'item_0' + brain + 's'}
       items.append(dict)
print(items)

Glad to see you solved it! A couple of minor considerations:
        - I would suggest you use a variable name other than 'dict', as that  
shadows the built-in 'dict' type
        - I would suggest using the idiom 'item_0%ss' % brain rather than  
'item_0' + brain + 's', in case at some point you want to change the type  
of 'brain'

--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

i greatly value your suggestion.
i have made changes as indicted by you.
The code looks more clean now.

Thank you,
Lee.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top