list of dataframe append having issue [duplicate] - python

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 1 year ago.
I am trying to create a list of dataframes.
I print my dataframe normally and it works fine.
print(df)
however if I create a list to store my df, it does not seem to work coz it prints 'None' from listOfDataframe.
# Create list of data frame using list
listOfDataframe = []
listOfDataframe = listOfDataframe.append(df)
print(listOfDataframe)
whats happening and How do I fix it?

There is no need to reassign a list after appending something to it. It changes it automatically:
# Create list of data frame using list
listOfDataframe = []
listOfDataframe.append(df)
print(listOfDataframe)
It is returning None simply because the append method returns None.
See Henry Ecker's comments.

Related

How to change the specific values within a dictionary of lists in python [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 1 year ago.
This is the worst blocker that I have ever had attempting to learn Python. I spent about 10 hours on this one and could really use help.
I want to have dictionaries hold lists in python and then access and change a specific value in those lists. When I do this the way that seems logical to me and access a key then list value, it actually replaces other key's list values.
If I just assign 100 to the key XList it prints 100 for both XList and YList. Why does assigning to one key effect the other???
And if I uncomment the assignment to YList it prints 254 for both. Why can't I assign itemized values to keys and lists like this? How can I assign specific values to lists within dictionaries and access the main lists via keys?? Why does what seems like changing just one keys list value change both key's lists???
testDictKeys= ['XList', 'YList', 'ZList']
testDict = dict.fromkeys(['XList', 'YList', 'ZList'])
noneFillerList = [None] * 30
#Now fill all the columns and row of the dictionary with zeros
for x in range(0,2):
testDict[testDictKeys[x]] = noneFillerList
for x in range(0, 29):
testDict['XList'][x]=100
#testDict['YList'][x]=254
print (testDict['XList'][0])
print (testDict['YList'][0])
Any help is appreciated.
The problem is that you only create one list and assign every dictionary to contain it. To fix the problem, be sure to create a new list to add to each dictionary.

Python 3.6 converting dictionary to a sorted list [duplicate]

This question already has answers here:
How to iterate over a python list?
(4 answers)
Closed 3 years ago.
I am trying to write a function in Python 3.6 that returns the content of a dictionary as a list, sorted according to the key in descending order.
Example test cases are as follows:
Inputs: {'y':1, 'z':1, 'x':1} | {3:['x','y'], 100:['z']}
Desired Outputs: [{'z':1},{'y':1},{'x':1}] | [{100:['z']}, {3:['x', 'y']}]
def sorted_dict_content(key_val):
# create an empty list
key_itemList = []
# for each key in sorted list of keys of key_val:
# append the key-value pair to key_itemList
for i in key_val:
key_itemList[key_val[i]].append(i)
sorted(key_val.key(), reverse=True)
return key_itemList
However, when I run the program I am getting IndexError: list index out of range. I also have not had luck with sorted().
Thanks for the help.
Trying to assign to the list via list[something] = something else will try to acces the list at the something-index not extend the list by adding the something-index, unlike a dict's behaviour. The correct way to append the key-value-pair to the list would be
key_ItemList.append({key_val[i]:i})

Unable to edit list after converting from tuple [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 4 years ago.
I'm using the code below:
a = ('Monty Python', 'British', 1969) #a is a tuple
b=list(a) #this should convert it to a list if I'm not wrong
print(b) #the output till here is okay
c=b.append("abcd")
print(c) # the output for this is None
Can anyone explain why am I unable to edit after converting the tuple to a list??
.append() does not return a list.
You are doing c = b.append("abcd"), this makes no sense because b.append() does not return a list, it returns none.
Try print(type(b.append("abcd"))) and see what it prints. So as you can see python is working correctly.
Things like .append() .pop() do not return a new list, they change the list in memory.
This is called an inplace operation I believe
You're printing c whose job is to append. Print b instead, that's your list.

Type error when trying to extend a list in Python [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 5 months ago.
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 a bit more explanations behind that.
You are storing the result of the list.append() or list.extend() method; both alter the list in place and return None. They do not return the list object again.
Do not store the None result; store the range() result, then extend or append. Alternatively, use concatenation:
years = range(2010, 2016) + [0]
years = [0] + range(2010, 2016)
Note that I'm assuming you are using Python 2 (your first example would not work otherwise). In Python 3 range() doesn't produce a list; you'd have to use the list() function to convert it to one:
years = list(range(2010, 2016)) + [0]
years = [0] + list(range(2010, 2016))
append and extend operations on lists do not return anything (return just None). That is why years is not the list you expected.
In Python 3 range returns an instance of the range class and not a list. If you want to manipulate the result of range you need a list, so:
years = list(range(2010,2016))
years.append(2016)
Finally, (and similarly to the append above) extend operates on the list you're calling it from rather than returning the new list so:
years = list(range(2010,2016))
years.extend( list(range(2016,2020)))
*While Python 2's range function does return a list, the above code will still work fine in Python 2*
Hope this helps!

Python - Extending a list directly results in None, why? [duplicate]

This question already has answers here:
How do I concatenate two lists in Python?
(31 answers)
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 5 months ago.
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 while trying to extend listC to listB.
listA.append([listB[15:18].extend(listC[3:12])])
Supposing lists cannot be directly appended / extending. What is the most popular work around form for resolving this issue?
list.extend modifies the list in place and returns nothing, thus resulting in None. In the second case, it's a temporary list that is being extended which disappears immediately after that line, while in the first case it can be referenced via x.
to append a listB to a listA while trying to extend listC to listB.
Instead of using extend, you might want to try this:
listA.append(listB[15:18] + listC[3:12])
Or do it in multiple simple lines with extend if you want to actually modify listB or listC.
extend will extend list it self. Return type of that method is None
If you want to union 2 list and add that list to another list then you have to use another way to add.
listB[15:18] = listC[3:12]
listA.extend(listB)

Categories