0

This is a question for one of my class assignments that I have tried solving for days...1) As you already know, we can have a list within a list in python. This feature enables us to make tables in python using lists. Create a list variable called student_info with the information below.

Name Color Lang Food

Arya Blue Python Sushi

Jon Yellow C++ Pasta

Sansa Green Java Taco

Bran Purple Julia Pizza

You can start by creating a seperate list for each row of the table by typing the sequence of data points of a row. Create the list by assigning it to a variable named row_1. Now let’s do the same for the other rows and create five lists, one for each row in our dataset. Finally append these rows to the table list called table_list.

In my code I tried this however I am stuck on how I am supposed to append multiple items into table_list. I cannot use for loops. This was my attempt but I keep getting assertion errors. Any help is appreciated.

student_info = ['Name','Color','Lang','Food'] row_1 = ['Arya','Blue','Python','Sushi'] row_2 = ['Jon','Yellow','C++','Pasta'] row_3 = ['Sansa','Green','Java','Taco'] row_4 = ['Bran','Purple','Julia','Pizza'] table_list = [] table_list.extend[row_1, row_2, row_3, row_4 ] 
1
  • try using .append rather than .extend. Commented Feb 4, 2021 at 6:18

4 Answers 4

2

Simply construct the list that you want:

table_list = [row_1, row_2, row_3, row_4] 
Sign up to request clarification or add additional context in comments.

Comments

1

That's because you forget () when you use extend() function!!! When you use function in python, don't forget the ().

student_info = ['Name','Color','Lang','Food'] row_1 = ['Arya','Blue','Python','Sushi'] row_2 = ['Jon','Yellow','C++','Pasta'] row_3 = ['Sansa','Green','Java','Taco'] row_4 = ['Bran','Purple','Julia','Pizza'] table_list = [] table_list.extend([row_1, row_2, row_3, row_4 ]) 

or just use:

table_list = [row_1, row_2, row_3, row_4 ] 

then table_list become

[['Arya', 'Blue', 'Python', 'Sushi'], ['Jon', 'Yellow', 'C++', 'Pasta'], ['Sansa', 'Green', 'Java', 'Taco'], ['Bran', 'Purple', 'Julia', 'Pizza']] 

Comments

1

The direct problem with your code is that methods/functions use parentheses not brackets. So strictly speaking, the line should be table_list.extend() not table_list.extend[].

With that in mind, .extend() will add the items of your list (removing the list structure), so you will not get a list of list, but one long list. You need to use .append() to get a list of list.

Regardless of the method you use, both .extend() and .append() only take one argument at a time. Without a for loop, you would have to call table_list.append(X), multiple times (or make X a list of your lists, which would be pointless in this specific scenario).

You can also use the more direct method of making list of lists by using table_list = [student_info, row_1, row_2, row_3, row_4]

Comments

-2

table_list=row_1+row_2+row_3+row_4

adding should work if you are trying to extend the list

Out[13]: ['Arya', 'Blue', 'Python', 'Sushi', 'Jon', 'Yellow', 'C++', 'Pasta', 'Sansa', 'Green', 'Java', 'Taco', 'Bran', 'Purple', 'Julia', 'Pizza']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.