-1

I graduated last May with an AAS in Information Technology, but during my last semester was listed as disabled due to my injuries in the service. I try to spend an hour each day learning Python, and my latest challenge has hit me hard--I just can't find the answer, even after an hour of searching the web for reprieve.

I challenged myself to make three lists; one and two, then the third with the second in the first. But the trick is, I have to do it within the terminal utilizing $user@pc python3. Here is the code I wrote:

# create list1
# filename = dumb_list
dumb_list = [3,5,7,9]

# create list2
#listname = dumber_list
dumber_list = [2,4,6,8,10]

# create list1 + list2, where list2 is inside of list1
# merge list1 into list2? Uh? How?

# // will research after trying manual merge //

# listname = dumbest_list
dumbest_list = [3,5,7,9,[2,4,6,8,10]]

# list3 = [list1[list1_contents]][list2[list2_contents]]
# list output in terminal should be from [list_i][list_i_contents]
# since 6 lies in the middle, let's go with it to pull from list3,
# so in terminal after typing lists, dumbest_list[2][2] should work
dumbest_list[2][2]

# --- failure ---
# Line 21 above, dumbest_list[2][2], fails...
# error message: Traceback (most recent call last):
  # File "<stdin>", line 1, in <module>
# TypeError: 'int' object is not subscriptable

Looking at Python documentation, it should pull the 6 from dumber_list within dumb_list using the call dumbest_list[2][2]. As you can see from my comments it does not. I have spent over an hour searching this and cannot understand what is happening. I even tried making each number a string, thinking the issue was number related, but the same error occurs. Whatever it is, I am not seeing it. None of the searches here help me see it. I am definitely missing something.

I tried using paramaters [2:][2:], too. Is the issue my list? Was typing it manually my failing? If so, I am unable to determine how to insert the list inside the other list. I can combine them into one list, but unable to insert it. I would list that code, too, but I overwrote it earlier. But if failed, too, and no searches there gave me reprieve either.

Can someone break this down to me? I want to understand it so I can document and read the python documentation and understand the do's and dont's involved with this.

And why the terminal? I dunno, it just felt like a fun way to test myself, so I won't change it. I wrote it out anyway, using Fedora 30 and Visual Studio Code 1.39.2. I also tried breaking it into simpler steps using Jupyter via Anaconda. I failed across the board.

i am root,

_rod

  • Please add your desired 3rd list for the example 1st and 2nd list you have given. – Sayandip Dutta Oct 18 '19 at 07:48
  • Try `dumbest_list[4][2]` – Trollsors Oct 18 '19 at 07:48
  • With respect, what you're asking is readily available in tutorials online - it doesn't take long to look. It also seems like you haven't really attempted to do the code yourself and are getting the internet to do it for you – EcSync Oct 18 '19 at 07:50
  • @EcSync That assumption is false. I found dozens of tutorials , but not one actually said "how". Even seeing it with my own eyes in some cases, they rushed through and the "how" was neglected. It wasn't until someone said to me, "So the second list, is at the first list's 4th position" that I realized what I was missing. I wasn't looking at the list inside as an item (i.e. I wasn't counting it, I started back at 0 like an idiot). But that's okay. I love learning moments like this. Now to find the documentation on it, that part is still avoiding me. – Rod Nintendeaux Oct 18 '19 at 12:50
  • you would use the append function, https://www.geeksforgeeks.org/append-extend-python/ this was after a few seconds of googling – EcSync Oct 18 '19 at 13:23

2 Answers2

0
# listname = dumbest_list
dumbest_list = [3,5,7,9,[2,4,6,8,10]]
# Lists start at 0. 
# So the second list, is at the first list's 4th position. 
# The 6 is at the second list's 2nd position
print(dumbest_list[4][2])
RightmireM
  • 2,381
  • 2
  • 24
  • 42
0

@RightmireM I cannot believe I didn't look at the second list as an item. I do not know what I was thinking, maybe exhaustion had finally hit me hard. Looking from that logic I see how I was missing it. I even found a tutorial that refers to it as 'nested lists' here. I wish that would have popped up on my search last night, would have saved me a lot of reading and watching videos of people flying through and not even explaining how the list fit into the list. Such simple logic I missed. Thank you, friend.