Question

Im trying to flatten out the following list in python code. I keep running into an...

Im trying to flatten out the following list in python code. I keep running into an 'int is not iterable error' i know this is due to the list being both strings and ints. how do i account for this? 


list_1 = [1,2,[3,[4,5],6],7,8,['hardware'],[['software'], 'interface']]


def flatten (list):
    flat = []
    for sublist in list_1:
        for item in sublist:
            flat.append(item)

    print(flat)
    return


flatten(list_1)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
list_1 = [1, 2, [3, [4, 5], 6], 7, 8, ['hardware'], [['software'], 'interface']]


def flatten(list_1):
    if type(list_1) == list:
        lst = []
        for item in list_1:
            lst.extend(flatten(item))
        return lst
    else:
        return [list_1]


print(flatten(list_1))

Add a comment
Know the answer?
Add Answer to:
Im trying to flatten out the following list in python code. I keep running into an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • while trying to run this in python, i keep getting the error TypeError: int() can't convert...

    while trying to run this in python, i keep getting the error TypeError: int() can't convert non-string with explicit base. any way to fix this? def binaryToDecimal(n): return int(n,2) if __name__ == '__main__': num3 = int(input("Enter value to be converted: ")) print(binaryToDecimal(num3))

  • I'm trying to run the code in Python below and keep getting invalid syntax. What's wrong?...

    I'm trying to run the code in Python below and keep getting invalid syntax. What's wrong? Thanks for any help! def even(n): if n%2==0 #Enter a Number def main(): n=int(input("Enter a number: ")) #If even print "Number is even" if(even(n)): print("The number is even") #If odd print "Number is odd" else: print("The number is odd") main() #Call the main function

  • Python question. How do i make this code work? im trying to print the value of...

    Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal class Node: def __init__(self, x): self.val = x self.left = None self.right = None class BinarySearchTree: def insertNode(self, value): self.root = self._insert(self.root, value)    def levelOrderTraversal(self, root): if root is None: return [] result, current = [], [root] while current: next_level, vals = [], [] for node in current: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) current...

  • I'm making a To-Do list in Python 2 and had the program running before remembering that...

    I'm making a To-Do list in Python 2 and had the program running before remembering that my professor wants me to put everything in procedures. I can't seem to get it to work the way I have it now and I can't figure it out. I commented out the lines that was originally in the working program and added procedures to the top. I've attached a screenshot of the error & pasted the code below. 1 todo_list = [] 2...

  • THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as...

    THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as an integer       a = int(input("Enter value for a: "))       #reading b as an interger       b = int(input("Enter value for b: "))       #reading n as an integer       n = int(input("Enter value for n: "))       # displaying if a and b are congruent modulo n, this is True if a and b       #both returns same remainder when divided by n       if a % n == b...

  •    im trying to print out my file I kept getting an error , i have...

       im trying to print out my file I kept getting an error , i have to to use iterator and algorthim         private:              std::string name;        std::list<practice> example;        public: void Write(std::ostream& os) {      os << name << std::endl;        std::for_each(example.begin(),example.end(), [&os](int& i)        {             os << i << std::endl;//here os is not capture        }); }

  • IN PYTHON 3 LANGUAGE, please help with function, USE RECURSION ONLY def im(l: 'an int, str,list,tuple,set,or...

    IN PYTHON 3 LANGUAGE, please help with function, USE RECURSION ONLY def im(l: 'an int, str,list,tuple,set,or dict') -> 'an int, str, tuple, or frozenset'      pass    SAMPLE OUTPUT: The following call (with many mutable data structures) imm(1)   returns 1 imm('a') returns 'a' imm( (1, 2, 3))   returns (1, 2, 3) imm( frozenset([1, 2, 3]))   returns frozenset({1, 2, 3}) imm( [1, 2, 3, 4, 5, 6])   returns (1, 2, 3, 4, 5, 6) imm( [1, 2, [3, [4], 5], 6])  ...

  • in python I have a list of strings, some contain purely numbers while others are dates,...

    in python I have a list of strings, some contain purely numbers while others are dates, names, etc. I am trying to convert the strings containing only numbers into floats. this is the code I am using but it alwyas returns nothing. Python just says restart and nothing goes through. how can I get my code to do what I want? f = open('GOOG-HistoricalQuotest.txt','r+') ff=f.readlines() clean = [] lst = [] for i in range(2,len(ff)+1): try: [float(i) for i in...

  • Can someone help with this C++ code. I am trying to compile and I keep running...

    Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. #include <iostream> #include <cassert> #include <string> using namespace std; typedef int fsm_state; typedef char fsm_input; bool is_final_state(fsm_state state) { return (state == 3) ? true : false; } fsm_state get_start_state(void) { return 0; } fsm_state move(fsm_state state, fsm_input input) { // our alphabet includes only 'a' and 'b' if (input != 'a' && input != 'b') assert(0); switch (state) {...

  • What will be printed upon running following code: alist = [3, 50, "Kity", [50, 52, "Rover"],...

    What will be printed upon running following code: alist = [3, 50, "Kity", [50, 52, "Rover"], [ ], 3.14, False, 'a'] print(len(alist[3]))alist = [3, 50, "Kity", [50, 52, "Rover"], [ ], 3.14, False, 'a'] print(len(alist[3])) 8 7 2 an error 3 Select the best reason why the code below returns True, False, False respectively? a = 256 b = 256 print(id(a) == id(b)) c = 5.1 d = 5.1 print(id(c) == id(d)) x = 257 y = 257 print(id(x) ==...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT