Python Forum
Questions on lists, the range function & concatenation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Questions on lists, the range function & concatenation
#1
Hi everyone.

2nd time poster so getting use to everything.

Here is what I got going on:
I am trying to create a list that looks like this:['a', 'b', 'c', ..., 'z'} so that I can then create a dictonary out of it.

I found a way to create that and the numbers 1-26

mport string
mylist = string.ascii_lowercase
a = "'"
b = "'"
c = ", "
for i in mylist:
    letters1 = a + i + b + c
    #print (letters1)

#Create range of numbers for merging
numbers = range(1,27)


But I am running into issues with the rest of the code I have which looks like this

#Create range of numbers for merging
numbers = range(1,27)
d = ":"
for n in numbers:
    numbers1 = str(n) + d
    print (numbers1)
I get an error when running the code and I can't seem to figure out how to get the numbers assigned to a list. If I just print numbers it says numbers = range (1,27) almost like it is treating it like a string instead of a function.

If I loop through I get an error about mistmatched types. If I loop through and try to convert those to a string (what is above) it also doesnt work.

If possible please explain this like you are talking to a 4 year old.

Thank you!
Reply
#2
import string


mylist = string.ascii_lowercase
for n, char in enumerate(mylist, 1):
    text = '{}:{}'.format(char, n)
    print(text)
I think you want to know what enumerate is.

Maybe you need at some point a function, which concatenates two iterables.

If you have two lists:

list1 = [1,2,3]
list2 = [4,5,6]
You can concatenate them with the function zip

for first, second in zip(list1, list2):
    print(first, second)
PS: Avoid the use of + operators to concatenate strings. In the most cases the format method is better.
More about formatting strings: https://docs.python.org/3.6/library/stdt...str.format
You get also here a good overview what format can do for you: https://pyformat.info/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Here is where I have gotten to now.

import string
letlist = list(string.ascii_lowercase)
#print (letlist)

#Create string of letters
letstring = string.ascii_lowercase
#print (letstring)

#Create list of numbers
form = ":"
list1=[]
for i in range (1, 27):
    i = str(i) + form
    list1.append(i)
#print (list1)
zipped = zip (letlist, list1)
print (zipped)


I get this error: <zip object at 0x00000186D8AFE048>

Not sure what is wrong with all that.
Reply
#4
zip() function returns an iterator (see here).
You need to iterate over it if you want the contents printed, as Dead_Eye showed in the last example of his previous post.
Reply
#5
Quick question on this board's ettiquete. Once I solve the problem should I post the working code or is that a waste of a post?

I have finished 2/3 of the problem and zip was very helpful.
Reply
#6
(Jan-25-2018, 02:49 PM)fad3r Wrote: Once I solve the problem should I post the working code or is that a waste of a post?
Followup replies about how you solve your problem are appreciated. Also it gives us an opportunity to tell you if there is a better way, or perhaps a method you aren't allowed to use in class but would be used in real life.
Reply
#7
It is very welcome to post your solution for other forum users to see.
Reply
#8
Ok thanks guys. Here is the code solving the first two parts (creating the values / lists & dictionaries)

import string
letlist = list(string.ascii_lowercase)

#Create string of letters
letstring = string.ascii_lowercase

#Create list of numbers
list1=[]
for i in range (1, 27):
    list1.append(i)

#Create dictionary for part 1
def dictfunc():
    zipped =dict(zip(list1,letlist))

dictfunc()

#Create dictionary for part 2
def dictfun2():
    zipped2 = dict(zip(list1, letstring))

dictfun2()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function for extracting data from lists Paulman 9 2,718 Nov-09-2021, 04:00 PM
Last Post: Paulman
  How can I run a function inside a loop every 24 values of the loop iteration range? mcva 1 2,101 Sep-18-2019, 04:50 PM
Last Post: buran
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,179 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Python Tutorial range function question HawkeyeKnight 2 2,659 Sep-27-2018, 09:45 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020