Python Forum
Slicing a complex dictionary and list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slicing a complex dictionary and list
#1
I’m trying to test my understanding of indexing and key calls.

I’ve got a variable which is a combination of two dictionaries and four lists. It’s confusing and not very practical but just for fun I want to print the very last list item.

Here is the variable:
d = {"levelone":[1,2,{'leveltwo':[5,6,[1,['get me please']]]}]}
Here is my attempt to slice the final list item:
print(d['levelone'][2]['leveltwo'][2][1][0])
My interpreter (Jupyter Notebook) shows this traceback:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-c2e6e77adf0d> in <module>()
----> 1 print(d['levelone'][2]['leveltwo'][2][1][0])

TypeError: string indices must be integers
This traceback is pointing to the slice of my list: ['levelone']. But it’s not actually a list slice, right? Why is it saying that the string slice must be an integer when it’s actually a dictionary key call? I figure I’m not properly referring to the first dictionary. According to the Dictionary tutorial by tutorialspoint, the valid syntax for pulling my first a key value would be d['levelone'].

Why is my interpreter saying there is an issue when there isn't?

Can someone please clarify?
Reply
#2
Just walk through it. I get no error.
d = {"levelone":[1,2,{'leveltwo':[5,6,[1,['get me please']]]}]}

print(d['levelone'])
print(d['levelone'][2])
print(d['levelone'][2]['leveltwo'])
print(d['levelone'][2]['leveltwo'][2])
print(d['levelone'][2]['leveltwo'][2][1])
print(d['levelone'][2]['leveltwo'][2][1][0])
99 percent of computer problems exists between chair and keyboard.
Reply
#3
It works for me. You're sure the error is at ['levelone']? You can be sure by doing the indexing one step at a time: start with d['levelone'], then d['levelone'][2], and so on. If it is at ['levelone'], then the interpreter things d is a string. Check d to make sure what it is (not print(d), but just d, or print(repr(d))).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 538 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 665 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 488 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 998 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,961 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,955 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,563 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,593 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,917 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Python dictionary with values as list to CSV Sritej26 4 2,990 Mar-27-2021, 05:53 PM
Last Post: Sritej26

Forum Jump:

User Panel Messages

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