Home Posts Topics Members FAQ
Join Bytes to post your question to a community of 471,995 software developers and data experts.

Post your reply
Sign in to post your reply or Sign up for a free account.
Similar topics
By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use .
To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.

python asp.net asp.net-mvc scala azure
Python assigning multiple variables to same list value? - python
I'm writing a function to calculate calendar dates. While cutting down on lines, I've found that I am unable to assign multiple variables to the same range. Jan, Mar, May, Jul, Aug, Oct, Dec = range(1,32) Would there be an efficient way to assign these values and why does python give a ValueError?
Use jan = mar = may = ... = range(1, 32), the easiest way to do what you described would be to use the x=y=z ... = value syntax, where x, y, z, and any other variables you include will all be assigned the value of value. in your example, all the comma-separated variables on the left side of the equals sign are assigned to sequential values of a tuple on the right side. hence, you could do something like this: values = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = values in your code, you have 7 values on the left, and 31 on the right, so you get an out of range error because the list on the left is longer than the number or variables on the left side to be assigned the values in it. i know the code above doesn't have much relevance to achieving your goal, but i thought i'd at least give some insight as to what it was trying to do. :d, python dictionary sort keys by a value with timestamp, normalizing timestamps in different formats to a common format, need to grab a 3x3 neighbourhood of an input cell from a 2d numpy array, python lambda if statements, python - for cycle in a list of lists [duplicate].
- Encrypt Message
Copyright © 2018 www.develop-bugs.com
Hello, buddy!
I'm a coder. Welcome to my blog. Here are some of the records on my job.
Python assigning multiple variables to the same list value?.
I'm writing a function to calculate calendar dates. While cutting down on lines, I've found that I am unable to assign multiple variables to the same range .
Would there be an efficient way to assign these values and why does python give a ValueError?
Related Articles
How to assign multiple variables of the same value in a smart way?, more elegant way to declare multiple variables at the same time, assign two variables to the same value with an expression?, sql - filling multiple variables in the same query, assigning multiple variables to the inspector, how can i pass multiple variables at the same time on this script, echo multiple variables in the same bash line, php set assigning multiple keys to the same value, google maps api v3 - assign multiple listeners to the same marker object, using the wherein method to return multiple results for the same table value but at a different index, python - check if multiple variables have the same value, multiplication element by element in an array of data - multiple variables at the same time (r), python: set many variables to the same value - with the automatic number of variables, do recursive methods create multiple variables of the same name?.
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- About the company

Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
python - read from file and assign into multiple lists
How do I make the code so it reads the file and splits it so that it can assign which lines I specify into a certain named list. I want the first 3 lines to be assigned into a list then the next 3 and so on, so it would be:
list1 = Steve, Cat, v, 2, 3, 4, 1, 28
list2 = John, Dog, h , 6, 1, 7, 2, 45
list3 = (something)
text file below named character:
main code below
Thanks for the help I'm new to python
- Is it always three lines? like first line is first name, 2nd line is last name, and 3rd line is attributes something like that? – VLDCNDN May 28, 2022 at 7:41
- yeah it's always 3 lines – Infusion May 28, 2022 at 7:45
- 1 Try to use with open("character.txt", 'r') as character_file: instead of open() and then close . It will close the file even if there's an exception. – Ignatius Reilly May 28, 2022 at 7:53
As mentioned in the comments, using a with block will close the file automatically once it goes out of scope.
This solution grabs your lines in blocks of 3. It then places the lists into a dictionary with the name as the key. It's not practical to put the lists into variables with the names as you have done.
As S3DEV has kindly mentioned in the comments, this is not very effective for large files and the file object itself can be iterated rather than reading in the entire file with readlines() . I've taken a shortcut and assumed that your file will always have 3 lines for each grouping and that your file will not end in the middle of an entry.
(note: walrus operator := requires Python 3.8 or above)
- 2 If reading a large file, the answer could be made more efficient by using a counter, rather than reading the entire file into memory. f can be iterated directly, thus removing the need to read the entire file into memory. – S3DEV May 28, 2022 at 9:22
- I wonder where readlines is still taught. In my opinion it should have been deprecated and thrown away when Python 3 was developed. – Matthias May 28, 2022 at 11:52
- I don't do much in the way of reading raw files anymore (csvs with Pandas and binary into NumPy, spoiled I know!). I'm assuming you mean using readline from f directly, but if I turn f into an iterator, there's no inherent way to just grab 3 off the top short of inslice that I found. – jonsca May 28, 2022 at 21:19
- @S3DEV Is that edit more in line with what you had in mind? (doing this more for my own edification at this point) – jonsca May 30, 2022 at 8:54
- 1 @jonsca - Even more simple actually. Something along the lines of the first solution in this answer . – S3DEV May 30, 2022 at 13:56
Your Answer
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged python list loops for-loop readline or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- The [amazon] tag is being burninated
Hot Network Questions
- Acidity of alcohols and basicity of amines
- Coasting Along the Coast
- Follow Up: struct sockaddr storage initialization by network format-string
- What is the correct way to screw wall and ceiling drywalls?
- Styling contours by colour and by line thickness in QGIS
- How to react to a student’s panic attack in an oral exam?
- Why do small African island nations perform better than African continental nations, considering democracy and human development?
- Finite abelian groups with fewer automorphisms than a subgroup
- Movie with vikings/warriors fighting an alien that looks like a wolf with tentacles
- What's the difference between a power rail and a signal line?
- What video game is Charlie playing in Poker Face S01E07?
- Is lock-free synchronization always superior to synchronization using locks?
- Most of the entries in the NAME column of the output from lsof +D /tmp do not begin with /tmp. Why is this the case?
- Does a summoned creature play immediately after being summoned by a ready action?
- Is it correct to use "the" before "materials used in making buildings are"?
- Is there a single-word adjective for "having exceptionally strong moral principles"?
- Minimising the environmental effects of my dyson brain
- ERROR: CREATE MATERIALIZED VIEW ... WITH DATA cannot be executed from a function
- Why is there a voltage on my HDMI and coaxial cables?
- Lots of pick movement
- Why are Suriname, Belize, and Guinea-Bissau classified as "Small Island Developing States"?
- Copyright issues when journal is defunct
- Recovering from a blunder I made while emailing a professor
- Can I tell police to wait and call a lawyer when served with a search warrant?
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .
Python List Comprehension: assign to multiple variables
Is there a way to assign to multiple variables in a one-liner?
Let's say I have a list of 3D points and I want x, y and z lists.
I am thinking of something like:
You can use zip() function:
Or if you want lists instead of tuples:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Click to generate QR
- Share to Weibo
- Copy to clipboard
python list comprehension and multiple variables
Multiple variables in list comprehension, python multiple list comprehension, python list comprehension with multiple 'if's, multiple conditions for list comprehension in python, python list comprehension with multiple loops, python: list comprehension with multiple conditions, list comprehension in python with multiple if statements, list comprehension with multiple regex with python, python list comprehension with multiple conditions, how to add multiple variables in list comprehension, enumerating three variables in python list comprehension, placing variables inside a python list comprehension, assign values with comprehension list based on list of tuples in base python, python list comprehension - replacing multiple letters in a string, python list comprehension loop over multiple collections, python list comprehension - generating multiple data, python - applying multiple conditions output in list comprehension, python dictionary comprehension: assign value to key, where value is a list, how does this code of list comprehension with multiple variables assigned works, python list comprehension: populating each inner-list with two variables, python assign multiple variables with map function, assign multiple variables in a collective string - python, how to assign serial data to multiple list of variables and averaged them for a specify duration in python, list comprehension with set comprehension in python, multiple list comprehension haskell, list comprehension with multiple arguments, multiple loop in list comprehension, related related.

IMAGES
VIDEO
COMMENTS
I am trying to return a list of indices for elements within another list that pass some sort of rule (as fast as possible). I am interested in the Elementspassed matrix just to be a list of indices in which
Join Stack Overflow to learn, share knowledge, and build your career. Sign up with email. Sign up with GitHub. Sign up with Facebook
Being familiar with the MATLAB programming language I'm looking for a convenient way to achieve the following assignment in python. A copy of the original list would also be fine
Because of pitfalls like this, code which modifies data in place can be more difficult to understand. However, it is often far more efficient to modify a large data structure in place than to create a modified copy for every small change
Being familiar with the MATLAB programming language I'm looking for a convenient way to achieve the following assignment in python
While cutting down on lines, I've found that I am unable to assign multiple variables to the same range. Jan, Mar, May, Jul, Aug, Oct, Dec = range(1,32) Would there be an efficient way to assign these values and why does python give a ValueError?
While cutting down on lines, I've found that I am unable to assign multiple variables to the same range. Python assigning multiple variables to the same list value?advertisements
How do I make the code so it reads the file and splits it so that it can assign which lines I specify into a certain named list
Is there a way to assign to multiple variables in a one-liner? Let's say I have a list of 3D points and I want x, y and z lists