Python Forum
For loops returning wrong output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loops returning wrong output
#1
I got a project going and it seems to give me an error.Im looking for the output of
a list what dict_subset returns. such as [{a:'Tim',b:'charlie','beta'},{a:'charlie',b :'Tim',c : beta}],and so on. this is the code.
    import string
    import random
    
    
    
    subset_list = [['charlie', 'Tim', 'beta'], ['Tim', 'charlie', 'beta'], ['beta', 'Tim', 'charlie'], ['Tim', 'beta', 'charlie'], ['charlie', 'beta', 'Tim'], ['beta', 'charlie', 'Tim']]
    dictionary_of_completed_values = []
    dict_subset = {}
    
    for every in subset_list:
            i = 0
            for each in every:
                dict_subset[string.ascii_letters[i]] = each
                i += 1
                dictionary_of_completed_values.append(dict_subset)
    
    for each in dictionary_of_completed_values:
        print(each)
Yoriz write May-03-2021, 09:50 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You only create one dictionary, so dictionary_of_completed_values ends up with 6 copies of the same dictionary. You need to create a new dictionary for each subset. This code does so with a dictionary comprehension.
import string

sub_list = [
    ['charlie', 'Tim', 'beta'],
    ['Tim', 'charlie', 'beta'],
    ['beta', 'Tim', 'charlie'],
    ['Tim', 'beta', 'charlie'],
    ['charlie', 'beta', 'Tim'],
    ['beta', 'charlie', 'Tim']]
sub_dict = []

for sub in sub_list:
    sub_dict.append({key:val for key, val in zip(string.ascii_letters, sub)})

for sub in sub_dict:
    print(sub)
If you don't comprehend comprehensions and don't take to zip, you should at least use enumerate.
import string

sub_lists = [
    ['charlie', 'Tim', 'beta'],
    ['Tim', 'charlie', 'beta'],
    ['beta', 'Tim', 'charlie'],
    ['Tim', 'beta', 'charlie'],
    ['charlie', 'beta', 'Tim'],
    ['beta', 'charlie', 'Tim']]
sub_dicts = []

for sub_list in sub_lists:
    sub_dict = {}
    for index, val in enumerate(sub_list):
        sub_dict[string.ascii_letters[index]] = val
    sub_dicts.append(sub_dict)

for sub_dict in sub_dicts:
    print(sub_dict)
Nighthound21 and Pedroski55 like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,468 Mar-27-2023, 07:38 AM
Last Post: buran
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 882 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,386 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Os command output in variable shows wrong value paulo79 2 1,467 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  print function output wrong with strings. mposwal 5 3,044 Feb-12-2021, 09:04 AM
Last Post: DPaul
  compiled cpython output wrong version bigrockcrasher 0 1,478 Feb-25-2020, 06:31 PM
Last Post: bigrockcrasher
  python gives wrong string length and wrong character thienson30 2 2,941 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Wrong output in Visual Studio Code py_learner 1 2,618 Jun-24-2019, 10:02 PM
Last Post: Yoriz
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,097 May-15-2019, 04:02 PM
Last Post: Aurimas
  Loops - new terminal output to file for each loop jm_ice 1 2,552 Dec-21-2018, 02:42 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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