Python Forum
Currency formatting the third element in a 2D list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Currency formatting the third element in a 2D list
#1
I've defined the third element in each list item as a float, but I want to convert them to currency format:

#!/usr/bin/env python3
#SamsLists.py

movies = [["Monty Python and the Holy Grail", 1975, 9.99],
          ["Uncle Bazerko's Violent Adventure", 2011, 9.99],
          ["The Expendables", 2010, 9.99],
          ["Duke Nukem: Fate of Humanity", 2019, 11.99]]

i = 0
for i in movies:
    movies[i][("${:,.2f}".format(movies[2]))]
    i += 1

for movie in movies:
    print(movie)

print()
print(movies)
The error was:
Error:
========= RESTART: I:/Python/Python36-32/SamsPrograms/MovieList2D.py ========= Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/MovieList2D.py", line 11, in <module> movies[i][("${:,.2f}".format(movies[2]))] TypeError: list indices must be integers or slices, not list >>>
My desired output is:
Output:
['Monty Python and the Holy Grail', 1975, $9.99] ["Uncle Bazerko's Violent Adventure", 2011, $9.99] ['The Expendables', 2010, $9.99] ['Duke Nukem: Fate of Humanity', 2019, $11.99]
What do I need to do?
Reply
#2
movies is a list of lists. So any "first level" indexing of movies will return a list.

movies = [
["Monty Python and the Holy Grail", 1975, 9.99],
["Uncle Bazerko's Violent Adventure", 2011, 9.99],
["The Expendables", 2010, 9.99],
["Duke Nukem: Fate of Humanity", 2019, 11.99]
]
And in the for loop:

i = 0
for i in movies:
    movies[i][("${:,.2f}".format(movies[2]))]
    i += 1
the integer i ("i=0") variable hasn't got much effect, and really you probably shouldn't be using a for loop like that anyway.
In each iteration i will be one of the nested lists from "movies" list, not the integer i.
Other thing is, if you want to change an item in the list, you should assign a new value to it:
movies[i][2] = "formatted movies[i][2]"
Reply
#3
(Mar-09-2018, 11:53 AM)j.crater Wrote: the integer i ("i=0") variable hasn't got much effect, and really you probably shouldn't be using a for loop like that anyway.
In each iteration i will be one of the nested lists from "movies" list, not the integer i.
Yeah, i is supposed to be one of the nested lists. Sorry, but I'm not quite sure what you're trying to point out here.

(Mar-09-2018, 11:53 AM)j.crater Wrote: Other thing is, if you want to change an item in the list, you should assign a new value to it:
movies[i][2] = "formatted movies[i][2]"
Well, I gave that my best shot:
#!/usr/bin/env python3
#SamsLists.py

movies = [["Monty Python and the Holy Grail", 1975, 9.99],
          ["Uncle Bazerko's Violent Adventure", 2011, 9.99],
          ["The Expendables", 2010, 9.99],
          ["Duke Nukem: Fate of Humanity", 2019, 9.99]]

i = 0
for i in movies:
    #movies[i][("${:,.2f}".format(movies[2]))]
    #movies["${:,.2f}".format(movies[i][2])]
    #movies[i]["${:,.2f}".format([2])]
    movies[i][2] = "${:,.2f}".format[i][2]
    i += 1

for movie in movies:
    print(movie)

print()
print(movies)
Error:
========= RESTART: I:/Python/Python36-32/SamsPrograms/MovieList2D.py ========= Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/MovieList2D.py", line 14, in <module> movies[i][2] = "${:,.2f}".format[i][2] TypeError: 'builtin_function_or_method' object is not subscriptable >>>
Reply
#4
Quote:Yeah, i is supposed to be one of the nested lists. Sorry, but I'm not quite sure what you're trying to point out here.
I don't blame you, it was a clumsy description on my part :) i "increments" on each loop iteration automatically, you don't need to assign a value to it, nor incremented it yourself (with i+=1).

You were on the right track, but the formatting function doesn't have the right syntax:

for movie in movies: 
    movie[2] = "${:,.2f}".format(movie[2])
movie is more descriptive than i. In this case it would be good to change name of "movies" also, since it's not too distinct from "movie". You may use any (valid) name though, "i" will work too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 590 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  currency converter using forex-python preethy12ka4 8 941 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  list in dicitonary element problem jacksfrustration 3 839 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,331 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 2,001 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,296 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  How to find the second lowest element in the list? Anonymous 3 2,148 May-31-2022, 01:58 PM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 2,117 May-11-2022, 06:05 PM
Last Post: deanhystad
  xml extract currency 3lnyn0 5 1,811 Apr-30-2022, 10:29 AM
Last Post: snippsat
  sorting a list of lists by an element leapcfm 3 2,001 Sep-10-2021, 03:33 PM
Last Post: leapcfm

Forum Jump:

User Panel Messages

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