This question already has answers here:
What is the difference between Python's list methods append and extend?
(20 answers)
Closed 2 years ago.
I am trying to rotate the array by using following function:
def rotLeft(a,d):
temp=[]
temp.append(a[0:-1])
temp.insert(0,a[-1])
return temp
I should get output as 5 1 2 3 4
but I am getting 5,[1,2,3,4]
how to solve this problem
You must use Use .extend() instead of .append() as .append() and .insert() is for adding elements while .extend() is to merge two lists:
def rotLeft(a,d):
temp=[]
temp.extend(a[0:-1])
temp.insert(0,a[-1])
return temp
print(rotLeft([1,2,3,4,5], 1))
output:
[5, 1, 2, 3, 4]
You need to use temp.extend, not temp.append. The latter just adds one element to temp, the list [1,2,3,4] - this is why you end up with a nested list. extend on the other hand works as if each element from [1,2,3,4] is appended to temp.
Related
This question already has answers here:
Get the first element of each tuple in a list in Python [duplicate]
(5 answers)
Closed 2 years ago.
I have the following code which gives me list as shown below
listP= methodcall()
print(listP)
Output [('abcd',), ('efgh',)
How do I remove bracket,comma and single quotes?
I need below output:-
listP=['abcd','efgh']
This will do the work:
listp = [('abcd',), ('efgh',)]
listp = [l[0] for l in listp]
print(listp)
Take only first element from the tuples in the original list using list comprehension.
newlist = []
list1 = [('abcd',), ('efgh',)]
for elements in list1:
newlist.append(elements[0])
print(newlist)
This question already has answers here:
What is the id( ) function used for?
(13 answers)
Closed 2 years ago.
I knew when python's list append element,the element is appended to tail. I tried to output the element of the list, why the element's address is out of order? Please help me out,thanks!
list = []
list.append(2)
list.append(10)
list.append(3)
print('--append--')
for i in list:
print('i:{}, id:{}'.format(i,id(i)))
the output is:
--append--
i:2, id:140711739437936
i:10, id:140711739438192
i:3, id:140711739437968
The id() function returns a unique id for the specified object.
You need to use the index of the list
list = []
list.append(2)
list.append(10)
list.append(3)
print('--append--')
for i in list:
print('i:{}, id:{}'.format(i,list.index(i))) # replace id with list.index
id() returns identity (unique integer) of an object...
a=3
print(id(3)) #9752224
You can use this
list = []
list.append(2)
list.append(10)
list.append(3)
print('--append--')
for i in enumerate(list): #enumerate return an enumerate object.if list it [(0,2),(1,10),(2,3)]
print('i:{}, id:{}'.format(i[1],i[0]))# for getting index number i[0]
The id function is rarely used in actual programming, and usually, you use the list index to deal with lists. Your example will look something like:
mylist = []
mylist.append(2)
mylist.append(10)
mylist.append(3)
print(mylist)
Output:
[2,10, 3]
Sample code:
for x in range(len(mylist)):
print(x, mylist[x])
Output:
0, 2
1, 10
2, 3
You may check one of the good python tutorials on the web such as the one on the python web page: https://docs.python.org/3/tutorial/
This question already has answers here:
Shortest way to slice even/odd lines from a python array?
(4 answers)
Closed 3 years ago.
I want to use list comp' instead of a loop.
Let's sat I have a list of lists and I want only the even index elements.
Eg: [[a,a], [b,b], [c,c],[g,g]] to..... [[a,a], [c,c]]
This doesn't work
a = [i for i in the_list if i % 2 == 0)]
Here is a possible solution that keeps only lists with even indexes:
result = [lst for i, lst in enumerate(the_list) if i % 2 == 0]
This question already has answers here:
Nested List Indices [duplicate]
(2 answers)
Having trouble making a list of lists of a designated size [duplicate]
(1 answer)
Closed 9 years ago.
I'm trying to figure out how to add a sub-list to a list in Python. For example, the code I was using before was just:
pop = [[],[],[],[],[],[],[],[],[],[]]
But I want to add user input to the length of the pop array, ie. how many arrays are added. I've looked at some other stackoverflow questions, and some of them suggested something like:
popLen = 5
pop = [None]*popLen
But when I try that it creates a list with 5 None elements instead of an empty array. I've tried:
pop = [[]]*popLen
What's the proper way to sub-lists?
pop = [[]]*popLen should work, but it is likely not what you want since it creates a list filled with the same nested list popLen times, meaning that a change to one of the list elements would appear in the others:
>>> a = [[]] * 3
>>> a[0].append(42)
>>> a
[[42], [42], [42]]
A better alternative would be
pop = [[] for _ in range(popLen)] # use xrange() in Python 2.x
which eliminates this issue:
>>> a = [[] for _ in range(3)]
>>> a[0].append(42)
>>> a
[[42], [], []]
You can do:
pop = [[] for x in range(popLen)]
Don't try to multiply [[]] - that will actually work, but will do something different than you expect (it will give you copies of the same list, so you cannot change them independently).
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13]
I need to obtain a specific length of elements form a list starting at a specific index in Python. For instance I would like to get the three next elements from the [2] element above. Is there anyway to get the three elements from the specific index? I wont always know the next amount of elements I want to get, sometimes I may want to get two elements, sometimes eight elements, so x elements.
I know I can do my_list[2:] to get all of the elements from the third element to the end of the list. What I want to do is specify how many elements to read after the third element. Conceptually in my mind the example above would look like my_list[2:+3] however I know this wont work.
How can I achieve this or is it better to define my own function to give me this functionality?
You are actually very close:
>>> my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13]
>>> x = 3
>>> my_list[2:2+x]
[3, 4, 5]
>>>
As you can see, the answer to your question is to slice the list.
The syntax for slicing is list[start:stop:step]. start is where to begin, stop is where to end, and step is what to count by (I didn't use step in my answer).
my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13]
n = 3
print my_list[2:2+n]
Nothing smart here. But, you can pull n out and tweak it the way you want.
you should simply use
my_list[2:2+3]
and in general
list[ STARTING_INDEX : END_INDEX ]
which is equivalent to
list[ STARTING_INDEX : STARTING_INDEX + LENGTH ]
>>> my_list = [1,2,3,4,5,6,7,8,9,10,11,12,13]
>>> my_list[2:3]
[3]
>>> my_list[2:2+3]
[3, 4, 5]