extend

In python, why can't I use the extend function inside the print function?

In python, why can't I use the extend function inside the print function? Question: If I write my code like this, friends = [“Kafi”, “Saif”, “Shaheer”, “Azmain”, “Labid”, “Faiz”, “Azmain”] lucky_numbers = [114, 151, 172, 7, 1, 63, 14, 543] friends.extend(lucky_numbers) print(friends) then my code runs perfectly. But if I use the extend function inside …

Total answers: 2

Append cumulative list of lists python

Append cumulative list of lists python Question: I am trying to extend a list of lists in a commulative way like this: # Consider the following list of lists l_Of_l = [ [1], [2], [3], [4], [5], [6], [7]] Desired result is: l_extended = [ [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], …

Total answers: 5

A way to insert a value in an empty List in a specific index

A way to insert a value in an empty List in a specific index Question: Is there a way to insert a specific value into a List into a specific index. List should be completely empty: L = [] L.insert(2,177) print(L) L should give out the values of L [ , ,117]. Asked By: user9935508 …

Total answers: 2

Type error when trying to extend a list in Python

Type error when trying to extend a list in Python Question: I need to understand why : years = range(2010,2016) years.append(0) is possible, returning : [2010,2011,2012,2013,2014,2015,0] and years = range(2010,2016).append(0) or years = [0].extend(range(2010,2016)) doesn’t work ? I understand that it is a type error from the message I got. But I’d like to have …

Total answers: 3

Python – Extending a list directly results in None, why?

Python – Extending a list directly results in None, why? Question: x=[1,2,3] x.extend(‘a’) Output: x is [1,2,3,’a’] But when I do the following: [1,2,3].extend(‘a’) Output: None Why does extend work on a list reference, but not on a list? 2nd Part: I found this because I was trying to append a listB to a listA …

Total answers: 2

User defined __mul__ method is not commutative

User defined __mul__ method is not commutative Question: I wrote a class to represent vectors in Python (as an exercise) and I’m having problems with extending the built-in operators. I defined a __mul__ method for the vector class. The problem is that in the expression x * y the interpreter calls the __mul__ method of …

Total answers: 2

What is the syntax to insert one list into another list in python?

What is the syntax to insert one list into another list in python? Question: Given two lists: x = [1,2,3] y = [4,5,6] What is the syntax to: Insert x into y such that y now looks like [1, 2, 3, [4, 5, 6]]? Insert all the items of x into y such that y …

Total answers: 6

Python extend with an empty list bug?

Python extend with an empty list bug? Question: Why does python 2.5.2 have the following behavior >>>[2].extend([]) == [2] False >>> [2].extend([]) == None True $ python –version Python 2.5.2 I assume I’m not understanding something here, but intuitively I’d think that [2].extend([]) should yield [2] Asked By: Doug T. || Source Answers: Extend is …

Total answers: 2

What is the difference between Python's list methods append and extend?

What is the difference between Python's list methods append and extend? Question: What’s the difference between the list methods append() and extend()? Asked By: Claudiu || Source Answers: append appends a single element. extend appends a list of elements. Note that if you pass a list to append, it still adds one element: >>> a …

Total answers: 20