Home > Blockchain >  A problem with dictionary (TypeError: string indices must be integers)
A problem with dictionary (TypeError: string indices must be integers)

Time:09-26

So, I am a new user of this site and I have a problem with dictionary function.

den = {
    'first_name': 'den',
    'last_name': 'elc',
    'age': '16',
    'city': 'slovakia',
}
for user, user_info in den.items():
    nameo = user_info['first_name']
    namei = user_info['last_name']
    age = user_info['age']
    city = user_info['city']
    print(f"\tFirst name:{nameo}")
    print(f"\tLast_name:{namei}")
    print(f"\tAge:{age}")
    print(f"\tCity:{city}")

Python prints this TypeError:

    Traceback (most recent call last):
  File "D:/PyCharm Community/pycharm/venv/Test.py", line 15, in <module>
    nameo = user_info['first_name']
TypeError: string indices must be integers

Any assumptions?

CodePudding user response:

In this case you don't need a for loop if you have an array of dictionaries then you need to consider using for loop. Here you can simply do like below.

den = {
    'first_name': 'den',
    'last_name': 'elc',
    'age': '16',
    'city': 'slovakia',
}
print(f"\tFirst name:{den['first_name']}")
print(f"\tLast_name:{den['last_name']}")
print(f"\tAge:{den['age']}")
print(f"\tCity:{den['city']}")

CodePudding user response:

You can use .title() and do this like below:

den = {
    'first_name': 'den',
    'last_name': 'elc',
    'age': '16',
    'city': 'slovakia',
}
for k,v in den.items():
    print(f'\t{k.title()}:{v}')

Output:

    First_Name:den
    Last_Name:elc
    Age:16
    City:slovakia

CodePudding user response:

The first problem here is that you are trying to extract string indices from a string itself. That is because when you iterate over den.items() you are iterating over its keys and values as user and user_info respectively.

So for every iteration of the loop -: user = key user_info = val

Now if you notice the dictionary you are using has values as strings. For e.g. if you have a string say 'hi'. Then this string has two indexes which can be only represented by integers 0, 1 or -1, -2. But what if we do this instead?

a = 'hi'
print(a['first_name'])

It will throw an error here because, indexes of a string as you might observe here cannot be a string.

Also another problem here is that from your code it seems like you require to loop over informations of multiple such users, but here you are doing it over a single user. To define it for multiple users, you need to define a larger dictionary with these dictionaries of user info as sub dictionaries. Like so-:

users = {
    {
     'first_name': 'den',
     'last_name': 'elc',
     'age': '16',
     'city': 'slovakia',
     },
    {
     'first_name': 'den_2',
     'last_name': 'elc_2',
     'age': '19',
     'city': 'romania',
    },
}

Now the old code of yours iterating over the items of this dict will work all fine!

for user, user_info in users.items():
    nameo = user_info['first_name']
    namei = user_info['last_name']
    age = user_info['age']
    city = user_info['city']
    print(f"\tFirst name:{nameo}")
    print(f"\tLast_name:{namei}")
    print(f"\tAge:{age}")
    print(f"\tCity:{city}")
  • Related